Update the JTable when I click button
In My application I have add a JTabel and using a setCellRender method I add a button column to the table as follows.
jTable1 = new JTable();
String[] columnNa开发者_开发知识库mes = {"Module", "Status", "Connectivity", "Action", "Start Time", "Logs"};
data = getServerExecutions();
model = new DefaultTableModel(data, columnNames) {
@Override
public boolean isCellEditable(int row, int col) {
return true;
}
};
jTable1.setModel(model);
jTable1.getColumn("Action").setCellRenderer(new ButtonRenderer());
jTable1.getColumn("Action").setCellEditor(new ButtonEditor(new JCheckBox()));
getSeverExecution Method load data from database table. When I click on Action button database get updated without any problems. Also there is a Refresh button in the window. Within the action performed method I reassign data to variabel data and refresh the table.
public void actionPerformed(ActionEvent e) {
data = getServerExecutions();
model.fireTableDataChanged();
}
but table data is not reloaded. Only when I close and open the window it get updated. what is wrong here?? Can some one pls help me..
This line:
data = getServerExecutions();
Does not set the data in the table. It just sets the data variable to point to the new data. You need to update the TableModel
instead. To do this either:
- Call the appropriate methods on your model
- Create a new model and set that model in the JTable
- or extend DefaultTableModel and create a way to update the data that the model runs off of
Seeing you recreate the data each time, I would just select the second choice.
精彩评论