user3650665
user3650665

Reputation: 51

How would I make a variable update from a row

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

Answers (1)

MadProgrammer
MadProgrammer

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

  1. TableModel#isCellEdtiable must return true for the specified column
  2. The TableCellEditor#isCellEditable must return true for the specified trigger event

That will allow the cell to be edited...

In order to update the data:

  1. 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 notification

Now, 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

Related Questions