Java JTable 重组/重新加载/刷新

发布于 2024-08-20 14:50:16 字数 1538 浏览 5 评论 0原文

我的面板上有一棵树和一张桌子,当我单击树节点时,桌子需要同时更改,但事实并非如此。我在网上搜索并阅读了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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

べ繥欢鉨o。 2024-08-27 14:50:16

在您的方法 displayFeedback 中,您似乎希望替换 JTable 对象并更改显示以反映上面 JTree 中选择的内容。您应该将精力集中在更新 Model 上,而不是替换 View 对象中的内容,在本例中,是您创建的 AbstractTableModel 子类。已创建。有几种方法可以做到这一点,但对于概念的强力证明,您可以执行如下操作:

  • MyTableModel 添加一个构造函数,该构造函数采用二维 数据数组
  • 在 displayFeedback 中,创建一个 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 the JTree above. Instead of replacing what is in the View object, you should focus your effort on updating the Model, in this case, the AbstractTableModel 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:

  • add a constructor to MyTableModel that takes a 2 dimensional array of data
  • in displayFeedback, create a new instance of MyTableModel that has new data relevant to the tree node that was selected.
  • call setModel on your global table variable.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文