我的面板上有一棵树和一张表,当我单击树节点时,表需要同时更改,但不需要更改。我在线搜索并阅读了Java教程,但没有找到任何解决方案。从某些帖子中,我认为我需要使用fireTableStruetureChanged()
,但是在我的代码中它不起作用。有人可以帮我吗?以下是代码。多谢!
public class tableStructureChange extends JFrame implements ... {
.....
/ //columnNames is a public variable, because I need to change the columns later
columnNames = new String[] {"col1","col2"}; */
data = new String[][]{
{"Mary", "Campione"},
{"Alison", "Huml"}, };
table = new JTable(new MyTableModel());
table.setAutoCreateColumnsFromModel( false );
feedback = new JScrollPane(table); //feedback is the bottom panel
...
}
//the following class is the problem, i need the table to be reloaded
//when the class is called, but the table doesn't change at all
public void displayFeedback(String tempString) {
//create table for bottom panel
columnNames = new String[] {"col3","col4", "col5"};
String[][] data = new String[][]{
{"Mary", "Campione", "us"},
{"Alison", "Huml", "canada"}, };
//table = new JTable(data, columnNames);
//fireTableStructureChanged(); //this is the problem part
}
//我的桌子型号类MyTableModel扩展了AbstractTableModel {String [] columnNames = new String [] {“ col1”,“ col2”};
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];
}
}
...}
在您的方法displayFeedback
中,您似乎希望替换JTable对象,并进行显示更改以反映在上面的JTree
中选择的内容。代替替换View对象中的内容,您应该将精力集中在更新Model(在这种情况下为已创建的AbstractTableModel
子类)上。您可以通过多种方式来执行此操作,但是对于强力概念验证,您可以执行以下操作:
MyTableModel
添加一个需要data
的二维数组的构造函数>MyTableModel
的新实例,该实例具有与所选树节点相关的新数据。setModel
变量上调用table
。 是葛特·鲁特。 Ich bin mla更广泛了!!!!!!!!