有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java在Netbeans中更改JTable的源代码并对其进行更新

我正在开发一个GUI,该GUI将具有一个已手动连接到数据库的表。此表上方有3个单选按钮,用于“决定”获取数据时要使用的标准(所有行都有一个布尔值,取决于按下的按钮,它应该返回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);

这是它应该做的,两个外部函数返回数组,使表显示它应该显示的内容(即程序启动所有“活动”事务时)

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

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

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

按下后输出,因此我知道在单击单选按钮后已调用此代码。这是TableRefresh函数

 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就在滚动窗格中

真诚地

//奥维尔·诺德斯特罗姆


共 (2) 个答案

  1. # 1 楼答案

    首先:

    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。请看一下这些答案,这里有一些解释来阐明这一点

    离题

    查看上面引用的行,我建议您避免使用静态成员。这些用于非常特定的情况(例如常数),但不用于一般用途,并且经常会破坏封装原则。在这种特殊情况下,它们导致了一系列难以理解的调用,这些调用可能是错误的,并导致(对我们来说)意外的难以调试的行为

  2. # 2 楼答案

    如果我理解您的问题是无法“刷新”表,那么在我的程序中,我使用以下方法(DefaultTableModel):

    private void jButtonActionPerformed(java.awt.event.ActionEvent evt) { .......
    ..................
    
    jTable1.setModel(new javax.swing.table.DefaultTableModel(
                new Object[][]{},
                new String[]{
                    "CLMN1", "CLMN2", "CLMN3", "CLMN..."
                }) {});
    enter code here
     model = (DefaultTableModel) jTable.getModel();
    
    model.addRow(new Object[]{("YOUR ROW"});   > in a While(or FOR), for any rows
    

    再见:)