Reputation: 51
I writing a program with a JTable
// I'm not going to write all the beginning stuff
DefaultTableModel model = new DefaultTableModel();
JTable t = new JTable(model);
Object[] 1 = {x, y, z};
model.addRow(1);
String s = 1
I was wondering how would I make it so if someone were to edit while the program was running how would I make so up date; so say y = 6
and then someone changed it to 8
how would I make s update to 8
too?
Upvotes: 0
Views: 44
Reputation: 347204
Start by taking a look at How to Use Tables and Using Other Editors for details about how to make a table editable.
There are a number of conditions that need to be meet...
TableModel#isCellEdtiable
must return true
for the specified columnTableCellEditor#isCellEditable
must return true
for the specified trigger eventThat will allow the cell to be edited...
In order to update the data:
TableModel#setValueAt
method must be capable of receiving the value from the editor and applying to the underlying data and then trigger the tableCellUpdated
event notificationNow, the good news is, for the most part, this is all setup by default by the JTable
and DefaultTabelModel
...
Try double clicking on the given cell, it should enter edit mode, you should be able to change it and press Enter and the value should be applied back to the data within the TableModel
Now, if you're interested in being notified when the TableModel
is changed, you will need to register a TableModelListener
with the TableModel
.
Upvotes: 3