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

标签 java swing user-interface jtable jradiobutton

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

enter image description here

这是表的代码(注意我正在使用 netbeans gui 设计器)

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);

这按其应有的方式工作,两个外部函数返回数组,使表格显示应显示的内容(即程序启动所有“Activity ”事务时)

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

这是我的单选按钮 Mousepressed 事件代码。

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 位于滚动 Pane 中..

真诚的...

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

最佳答案

作为一个开始:

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/

相关文章:

c - 使用 C 编写可用窗口

java-为什么我的 "deposit"方法不适用于带有 gui 的 ATM?

matlab - 在 matlab 中追踪包含背景图像的图形

java - Hibernate 映射与动态查询

java - HTML 解析和删除 anchor 标记,同时使用 Jsoup 保留内部 html

java - Swing自定义绘画动画在达到框架的一半宽度后停止

java - 如何使用 Java Print API 确保页面尺寸

java - 修复 Java 7 中 swing 原始类型的使用

java - 使用带有 Netbeans IDE 的 Java Swing 创建 GUI

java - 为什么我得到 "non-static variable this cannot be referenced from a static context"?