Refresh a jTable

北城余情 提交于 2020-01-22 02:15:11

问题


I can't seem to get my table to refresh. I created a refresh button that calls jTable1.repaint();

private void jButton8ActionPerformed(java.awt.event.ActionEvent evt)        
// Reader Refresh
        jTable1.repaint();
    }

I also tried just recalling the RegistryValues again in the button like RegistryValues.arp(null);

private void jButton8ActionPerformed(java.awt.event.ActionEvent evt)        
// Reader Refresh
        RegistryValues.arp(null);
    }

Also tried combining the registryvalues and repaint in the button.

Below is the code for my jTable. The RegistryValues are from another class that uses JNA to read the registry if that matters.

jTable1.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {"Protected Mode at Startup", RegistryValues.arp(null)},
                {"Display PDF in browser", RegistryValues.arb(null)},
                {"EULA Accepted?", RegistryValues.are(null)},
                {null, null}
            },
            new String [] {
                "Software", "Status"
            }
        ));

回答1:


Neither

jTable1.repaint();

or

RegistryValues.arp(null);

will actually refresh the table with new values. For this you need to either update the current table model or set a new model but in your ActionListener.

As you're using DefaultTableModel, which is mutable, you could create an update helper method for the model.

Something like:

DefaultTableModel model = (DefaultTableModel) table.getModel();
model.setValueAt(RegistryValues.arp(null), 0, 1);
// set more row data, etc.

Note: You could save the model as a class member variable and eliminate the need for casting.



来源:https://stackoverflow.com/questions/12078061/refresh-a-jtable

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!