Table with CustomModel when clicked shows old entry
Hi,
I have a problem with a Table using a CustomModel I have created.
The first time everything works fine, but whenever I re-create the Table and the user clicks on it, the content of the table changes into the old ones.
Here is a part of the code.
I cannot provide a SSCE for the moment, but I can give an idea of the whole thing.
Code :
private static void showResults() {
// Creating JTable
CustomTable Table = new CustomTable();
}
public static class CustomTable extends JPanel {
public CustomTable() {
CustomTableModel TableModel = new CustomTableModel();
JTable Table = new JTable(TableModel);
// Create the scroll pane and add the table to it.
JScrollPane scrollPane = new JScrollPane(Table);
Table.setFocusable(false);
// Add the scroll pane to this frame
Frame.add(scrollPane, BorderLayout.CENTER);
scrollPane.setBounds(100, 300, 600, 250);
}
class CustomTableModel extends AbstractTableModel {
private String rows[][] = Search();
private String headers[] = { "", "", "", "", ""};
public int getColumnCount() {
return headers.length;
}
public int getRowCount() {
return rows.length;
}
public String getColumnName(int col) {
return headers[col];
}
public Object getValueAt(int row, int col) {
return rows[row][col];
}
// Table not editable
public boolean isCellEditable(int row, int col) {
return false;
}
}
Basically I recreate the Table every time the user clicks on a button for searching. The results are correct but whenever I click on the table, the cell/row I click on changes its values on the first one.
I also tried to remove the possibility of selecting an item of the Table, but the problem still remains.
I also tried to remove it from the Frame (using Frame.remove(Table) ) and revalidate it. Also repaint and so on. Also tried to remove all the content of the Table before recreating a new one, but nothing...
Re: Table with CustomModel when clicked shows old entry
Quote:
the cell/row I click on changes its values on the first one
Try overriding the setValueAt in AbstractTableModel to set the appropriate value
Re: Table with CustomModel when clicked shows old entry
Tried, but same problem:
Code :
public void setValueAt(int row, int col)
{
super.setValueAt(Table, row, col);
}
Re: Table with CustomModel when clicked shows old entry
See the API for AbstractTableModel - that is not overriding the setValueAt method.
Re: Table with CustomModel when clicked shows old entry
I have googled for a while and tried some ways to override the setValueAt method, I tried with:
Code :
public void setValueAt(Object value, int row, int col) {
rows[row][col] = value.toString();
this.fireTableCellUpdated(row, col);
}
But the result is still the same.
I just wanted to give an update, I will try to search for more...
Re: Table with CustomModel when clicked shows old entry
Quote:
Originally Posted by
Nesh108
Hi,
Code :
class CustomTableModel extends AbstractTableModel {
private String rows[][] = Search();
}
The above section caused the problem. The rows object is initialized only once by the Search() function. You should re-design the CustomTableModel, for example, adding some methods to update the search result.
java exception
Re: Table with CustomModel when clicked shows old entry
Thanks!!!!
Oh finally!!!
here how I fixed the problem:
Code :
//Update
public void updateResult(){
rows =Search();
this.fireTableDataChanged();
}
In this way I do not re-create the table each time I search for new results.