gpt4 book ai didi

java - 如何使用 Eclipse 选择 jtable 行

转载 作者:行者123 更新时间:2023-12-04 08:30:48 26 4
gpt4 key购买 nike

我是 Eclipse 的初学者。我正在创建一个简单的 crud 系统。成功加载 JTable选择行选定的行记录将显示在相关的文本字段上以进行编辑目的。到目前为止我尝试过的内容附在下面。
我这里选择的事件是选择JTable单击鼠标右键单击事件处理程序但不会显示任何内容。

scrollPane = new JScrollPane();
scrollPane.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
df = (DefaultTableModel)table_1.getModel();
int selected = table_1.getSelectedRow();
int id = Integer.parseInt(df.getValueAt(selected, 0).toString());
txtname.setText(df.getValueAt(selected, 1).toString());
txtmobile.setText(df.getValueAt(selected, 2).toString());
txtcourse.setText(df.getValueAt(selected, 3).toString());
btnsave.setEnabled(false);
}
屏幕截图
enter image description here

最佳答案

请注意,在 JTable 上单击鼠标右键不选择行。
这是你想要的吗?

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.EventQueue;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import java.awt.GridBagConstraints;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.ListSelectionModel;
import javax.swing.WindowConstants;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableModel;

public class StudCrud implements Runnable, ListSelectionListener {
private JFrame frame;
private JTable table;
private JTextField nameTextField;
private JTextField phoneTextField;
private JTextField addressTextField;

@Override // javax.swing.event.ListSelectionListener
public void valueChanged(ListSelectionEvent event) {
if (!event.getValueIsAdjusting()) {
int selectedRow = table.getSelectedRow();
if (selectedRow >= 0) {
TableModel model = table.getModel();
Object obj = model.getValueAt(selectedRow, 1);
nameTextField.setText(obj == null ? "" : obj.toString());
obj = model.getValueAt(selectedRow, 2);
phoneTextField.setText(obj == null ? "" : obj.toString());
}
}
}

@Override // java.lang.Runnable
public void run() {
showGui();
}

private JPanel createButtons() {
JPanel buttonsPanel = new JPanel(new GridLayout(0, 4, 5, 0));
JButton saveButton = new JButton("Save");
buttonsPanel.add(saveButton);
JButton updateButton = new JButton("Update");
buttonsPanel.add(updateButton);
JButton deleteButton = new JButton("Delete");
buttonsPanel.add(deleteButton);
JButton exitButton = new JButton("Exit");
buttonsPanel.add(exitButton);
return buttonsPanel;
}

private JPanel createForm() {
JPanel form = new JPanel(new GridBagLayout());
form.setBorder(BorderFactory.createLineBorder(Color.GRAY));
GridBagConstraints gbc = new GridBagConstraints();
gbc.anchor = GridBagConstraints.LINE_START;
gbc.gridx = 0;
gbc.gridy = 0;
gbc.insets.bottom = 5;
gbc.insets.left = 5;
gbc.insets.right = 5;
gbc.insets.top = 5;
JLabel nameLabel = new JLabel("Student Name");
form.add(nameLabel, gbc);
gbc.gridx = 1;
nameTextField = new JTextField(10);
form.add(nameTextField, gbc);
gbc.gridx = 0;
gbc.gridy = 1;
JLabel phoneLabel = new JLabel("Phone");
form.add(phoneLabel, gbc);
gbc.gridx = 1;
phoneTextField = new JTextField(10);
form.add(phoneTextField, gbc);
gbc.gridx = 0;
gbc.gridy = 2;
JLabel addressLabel = new JLabel("Address");
form.add(addressLabel, gbc);
gbc.gridx = 1;
addressTextField = new JTextField(10);
form.add(addressTextField, gbc);
gbc.gridx = 0;
gbc.gridy = 3;
gbc.gridwidth = 2;
form.add(createButtons(), gbc);
return form;
}

private JScrollPane createTable() {
table = new JTable(createTableModel());
table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
ListSelectionModel selectionModel = table.getSelectionModel();
selectionModel.addListSelectionListener(this);
JScrollPane scrollPane = new JScrollPane(table);
return scrollPane;
}

private TableModel createTableModel() {
Object[] columnNames = {"id", "name", "mobile", "course"};
Object[][] data = {{"1", "suren", "883", "php"},
{"2", "asd", "232", "sda"},
{"3", "nisa", "8999", "bads"},
{"4", "yt", "45", "fgh"}};
TableModel model = new DefaultTableModel(data, columnNames);
return model;
}

private void showGui() {
frame = new JFrame("Student Crud");
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
((BorderLayout) frame.getContentPane().getLayout()).setHgap(10);
frame.add(createForm(), BorderLayout.LINE_START);
frame.add(createTable(), BorderLayout.LINE_END);
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
}

public static void main(String[] args) {
EventQueue.invokeLater(new StudCrud());
}
}
使用最合适的 listener ,根据我对您问题的理解,是 ListSelectionListener .
也许还阅读 How to Use Tables以及 Laying Out Components Within a Container .
这是它应该看起来的样子。
screen capture

关于java - 如何使用 Eclipse 选择 jtable 行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65032642/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com