简体   繁体   中英

JTable updating cell value after editing

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);  
        }
    };

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!!!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM