在Netbeans中更改JTable的源并更新它

时间:2014-12-02 14:29:56

标签: java swing user-interface jtable jradiobutton

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

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个外部函数返回数组,使表显示它应该显示的内容(这是程序启动所有“活动”事务)

但是我希望能够更改表格,以便评估放射性是否等于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在Scrollpane中。

...诚恳

//OrvilleNordström。

2 个答案:

答案 0 :(得分:2)

就像一个开始:

  

如果它有任何区别,JTable在Scrollpane中。

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

  

那么在应用了不同的获取数据标准后,我应该如何更新表格呢?

代码报价:

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。请查看这些答案,有解释说清楚。

题外话

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

答案 1 :(得分:1)

如果我理解你的问题是你不能“刷新”表格,在我的程序中我使用这种方法(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
再见:)