gpt4 book ai didi

java - 在 Netbeans 中更改 JTable 的源代码并更新它

转载 作者:行者123 更新时间:2023-11-30 08:18:45 25 4
gpt4 key购买 nike

我正在开发一个 GUI,它将包含一个已手动连接到数据库的表。在此表上方有 3 个单选按钮,它们将“决定”在获取数据时使用什么标准(所有行都有一个 boolean 值,具体取决于按下的按钮,它应该返回 1、0 或两者)。

enter image description here

这是表格的代码(注意我使用的是 netbeans gui designer)

ServiceTable = new javax.swing.JTable();
int radiovalue = 0;
if (RadioActive.isSelected()) radiovalue = 0;
else if (RadioAll.isSelected()) radiovalue = 1;
else if (RadioFinished.isSelected()) radiovalue = 2;
Object[][] DataAct = null;
try {
DataAct = SQL.MYSQL_FETCH_OMNI_DATA(radiovalue);
} catch (Exception ex) {
Logger.getLogger(MainforAdmin.class.getName()).log(Level.SEVERE, null, ex);
}
String[] Colnombs = SQL.MYSQL_ROW_NOMBER();
ServiceTable.setAutoCreateRowSorter(true);

ServiceTable.setModel(new javax.swing.table.DefaultTableModel( DataAct, Colnombs ));

TableContainer.setViewportView(ServiceTable);

这是正常工作的,2 个外部函数返回数组,使表格显示它应该显示的内容(这是程序启动所有“Activity ”事务)

但是我希望能够更改表格,以便它能够评估放射性是否等于 0、1 或 2(该数字将确定函数获取的数据)。该程序通过 System.out.print 输出符合不同标准的 MYSQL 表。所以我知道我的功能正在运行。但是我不知道如何在选择另一个单选按钮后使整个表格“刷新”..

这是我为单选按钮按下鼠标的事件代码。

TableRefresher();
System.out.println("Pressed");

然后输出 Pressed,所以我知道在单击单选按钮后已调用此代码。这是 TableRefresher 函数。

 Write.Echo("The TableRefresher method hath been summoned");
//This code is going to evaluate which button is selected as of now.
MainforAdmin table = new MainforAdmin();
if (table.RadioActive.isSelected()) radiovalue = 0;
else if (table.RadioAll.isSelected()) radiovalue = 1;
else if (table.RadioFinished.isSelected()) radiovalue = 2;
Object[][] DataAct = null; //This code is going to innitialize the tablecontents.
try {
DataAct = SQL.MYSQL_FETCH_OMNI_DATA(radiovalue);//This code assigns the table contents And i know this works because it correctly outputs the table with the diffrent where clause (where state = x or the status as you saw on the picture.)
} catch (Exception ex) {
Logger.getLogger(MainforAdmin.class.getName()).log(Level.SEVERE, null, ex);
}
String[] Colnombs = SQL.MYSQL_ROW_NOMBER(); //Assigns the column names and works is used as the table is created so this works.
table.TableContainer.remove(table.ServiceTable);
table.add(table.ServiceTable, null);
table.ServiceTable.setModel(new javax.swing.table.DefaultTableModel( DataAct, Colnombs ));
table.ServiceTable.revalidate();
table.ServiceTable.repaint();
table.TableContainer.setViewportView(table.ServiceTable);

然而,当此方法被调用时(我知道它来自控制台输出),GUI 中的 JTable 没有任何变化...它保持不变。

那么在应用不同的数据获取标准后,我应该如何刷新表格呢?我已经查看了本网站上的其他建议,但没有一个有效或提供了我需要的东西。

任何答案都将不胜感激,如果这是一个简单的问题,请原谅我,我绝不是编程神。

如果 JTable 在 Scrollpane 中有任何不同..

真诚的...

//奥维尔·诺德斯特伦。

最佳答案

作为开始:

If it makes any difference the JTable is in a Scrollpane.

这是正确的,它必须保持这种方式。尽管解决主要问题没有区别。

So how am i supposed to refresh the table after a different criteria for fetching the data has been applied?

代码引用:

table.TableContainer.remove(table.ServiceTable);
table.add(table.ServiceTable, null);
table.ServiceTable.setModel(new javax.swing.table.DefaultTableModel( DataAct, Colnombs ));
table.ServiceTable.revalidate();
table.ServiceTable.repaint();
table.TableContainer.setViewportView(table.ServiceTable);

请注意,这是一种意大利面条代码,不清楚要更新哪个表。然而,更新表的正确方法不是删除/重新创建/重新定位它,而是使用/刷新它的表模型。查看示例 here (包括在后台线程中进行数据库调用的 SwingWorker),herehere .请查看这些答案,其中有解释清楚这一点。

题外话

查看上面引用的行,我建议您避免使用静态成员。这些是针对非常特殊的情况(即常量)而不是一般用途,并且经常破坏封装原则。在这种特殊情况下,它们导致了一个无法理解的调用链,这些调用链可能是错误的,并导致(对我们而言)意想不到的难以调试的行为。

关于java - 在 Netbeans 中更改 JTable 的源代码并更新它,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27251837/

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