Reputation: 25
Why my table doesn't update after I set values.I have setValuesAt method but it does not work.
String[] ColumnNames = {"Register","Value"};
String[][] data = new String[Integer.parseInt(Amount_Text_Field.getText())][ColumnNames.length];
DefaultTableModel model = new DefaultTableModel(data, ColumnNames)
{
private static final long serialVersionUID = 1L;
@Override
public boolean isCellEditable(int row, int column)
{
return true;
}
@Override
public void setValueAt(Object value, int row, int column) {
data[row][column]=(String) value;
fireTableCellUpdated(row, column);
}
};
Upvotes: 0
Views: 950
Reputation: 324098
There is no need to override the setValueAt() method of the DefaultTableModel. The DefaultTableModel already implements that method.
String[][] data = new String[Integer.parseInt(Amount_Text_Field.getText())][ColumnNames.length];
I would guess your text fields don't contain any data when the above statement is invoked. So there is no data to be added to the table.
If you want the use to enter data into a text field then you need to have a button like "Add data". Then in the ActionListener
of the button you do something like:
String[] row = new String[Integer.parseInt(Amount_Text_Field.getText())][ColumnNames.length];
model.addRow( row );
Also, variable names should NOT start with an upper case character. Some are correct other are not. Be consistent!!!
Upvotes: 3