Java JTable 重组/重新加载/刷新
我的面板上有一棵树和一张桌子,当我单击树节点时,桌子需要同时更改,但事实并非如此。我在网上搜索并阅读了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[] 列名称 = 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];
}
}
... }
I have a tree and a table on my panel, when I click the tree node, the table needs to change at the same time, but it doesn't. I search online and read the Java tutorial and didn't find any solutions. From some posts I think I need to use fireTableStruetureChanged()
, but it just doesn't work in my code. Could anyone help me out of this? The following is the code. Thanks a ton!
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
}
// my table model
class MyTableModel extends 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];
}
}
...
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在您的方法
displayFeedback
中,您似乎希望替换 JTable 对象并更改显示以反映上面JTree
中选择的内容。您应该将精力集中在更新 Model 上,而不是替换 View 对象中的内容,在本例中,是您创建的AbstractTableModel
子类。已创建。有几种方法可以做到这一点,但对于概念的强力证明,您可以执行如下操作:MyTableModel
添加一个构造函数,该构造函数采用二维数据数组
MyTableModel
的新实例,其中包含与所选树节点相关的新数据。table
变量调用setModel
。In your method
displayFeedback
you seem to be hoping to replace the JTable object and have the display change to reflect what is selected in theJTree
above. Instead of replacing what is in the View object, you should focus your effort on updating the Model, in this case, theAbstractTableModel
subclass that you have created. There are a couple ways you can do that, but for a brute force proof of concept, you could do something like the following:MyTableModel
that takes a 2 dimensional array ofdata
MyTableModel
that has new data relevant to the tree node that was selected.setModel
on your globaltable
variable.