如何更改以下代码来更新 JTable?
我想单击搜索按钮并更改列数和行数。并为每个单元格设置值。
但在单击搜索按钮后,它会显示第二个 JTable 而不是更新第一个 JTable。不知道是什么问题。
import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JSplitPane;
import javax.swing.JTable;
import javax.swing.table.AbstractTableModel;
public class Test extends JFrame {
Container c;
public Test(){
c= getContentPane();
c.setLayout(new BorderLayout());
//create table
JTable table = new JTable(new MyTableModel());
JScrollPane scrollPane = new JScrollPane(table);
//create search button
JButton search = new JButton("search");
c.add(scrollPane, BorderLayout.WEST);
c.add(search, BorderLayout.EAST);
//create splitter
JSplitPane splitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, scrollPane, search);
splitPane.setResizeWeight(0.7);
add(splitPane, BorderLayout.CENTER);
//ActionListener
ListenerSearch listenersearch = new ListenerSearch();
search.addActionListener(listenersearch);
}
public class ListenerSearch implements ActionListener {
public void actionPerformed(ActionEvent e) {
String[] columnNames = {"First Name",
"Last Name",
"Sport",
"# of Years",
"Vegetarian","One More Column"};
MyTableModel changedtable = new MyTableModel();
changedtable.setString(columnNames);
Object[][] data = {
{"new", "value",
"new", new Integer(10), "false", "new column"},
{"new", "value",
"new", new Integer(3), "true", "new column"},
{"new", "value",
"new", new Integer(2), "true", "new column"},
{"new", "value",
"new", new Integer(20), "true", "new column"},
{"new", "value",
"new", new Integer(10), "true", "new column"},
{"new", "value",
"new", new Integer(5), "true", "new column"},
{"new", "value",
"new", new Integer(3), "true", "new column"},
{"new", "value",
"new", new Integer(2), "true", "new column"},
{"new", "value",
"new", new Integer(20), "true", "new column"},
{"new", "value",
"new", new Integer(10), "true", "new column"}
};
changedtable.setObject(data);
JTable table = new JTable(changedtable);
JScrollPane scrollPane = new JScrollPane(table);
c.add(scrollPane, BorderLayout.WEST);
c.revalidate();
}
}
public static class MyTableModel extends AbstractTableModel {
public String[] columnNames = {"First Name",
"Last Name",
"Sport",
"# of Years",
"Vegetarian"};
public void setString(String[] columnNameshelper){
columnNames=columnNameshelper;
}
public Object[][] data = {
{"Kathy", "Smith",
"Snowboarding", new Integer(5), "true"},
{"John", "Doe",
"Rowing", new Integer(3), "true"},
{"Sue", "Black",
"Knitting", new Integer(2), "true"},
{"Jane", "White",
"Speed reading", new Integer(20), "true"},
{"Joe", "Brown",
"Pool", new Integer(10), "true"}
};
public void setObject(Object[][] datahelper){
data=datahelper;
}
public int getColumnCount() {
return columnNames.length;
}
public int getRowCount() {
return data.length;
}
public String getColumnName(int col) {
return columnNames[col];
}
public Object getValueAt(int row, int col) {
return data[row][col];
}
public Class getColumnClass(int c) {
return getValueAt(0, c).getClass();
}
public boolean isCellEditable(int row, int col) {
if (col < 2) {
return false;
} else {
return true;
}
}
public void setValueAt(Object value, int row, int col) {
data[row][col] = value;
}
}
public static void main(String[] args) {
Test fenster = new Test();
fenster.setSize(800,400);
fenster.setVisible(true);
fenster.setTitle("Test");
fenster.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
最佳答案
将表格存储在一个变量中(就像您使用“Container c”所做的那样,并在 Action 监听器中更改它,而不是将新表格添加到容器中。
[...]
public class Test extends JFrame {
Container c;
JTable t
[...]
// In constructor:
t = new JTable(new MyTableModel());
[...]
// In method actionPerformed
// Do not create a new table, but instead fill the existing one with the changed data
t.setModel(changedtable);
}
关于java - 如何使用 ActionListener 更改 JTable,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31990469/