Home Upgrade Search Memberlist Extras Hacker Tools Award Goals Help Wiki Contact

Java 

Updating data in JTable when object changes.

Hey.
Let's say I have this JTable:

Code
private JScrollPane scrollPane1;
      private JTable table;
      private DefaultTableModel model;

      DefaultTableModel model = new DefaultTableModel();
      model.addColumn("Item");
      model.addColumn("ID");
      model.addColumn("Price");
      model.addColumn("Category");

      this.scrollPane1 = new JScrollPane();
      this.scrollPane1.setBounds(33, 518, 604, 300);
      this.contentPane.add(this.scrollPane1);
      table = new JTable(model);
      this.scrollPane1.setViewportView(table);

I have an Item class, so when I create a new instance of Item, I add the items info to the JTable:
Code
Item item = new Item("Shovel", 1, 123, "Tools");
model.addRow(new Object[]{item.getItem(), item.getID(), item.getPrice(), item.getCategory()});
So far so good.
But if I update the Item, for example change the price from 5 to 10, it is not updated in the JTable. So if I for example do:
Code
item.setPrice(10);
It doesnt update in the JTable. I've tried calling model.fireTableDataChanged(); and table.repaint(); but none of them works. It seems like the value in the cell is not associated with the objects value? How would I do this? (First time working with JTables).

Any suggestions? Don't be bothered by the lack of methods etc., I just put this together quickly for this post.
This is a Stack Overflow answer:
If you want to notify your JTable about changes of your data, use
tableModel.fireTableDataChanged()

From the documentation:

Notifies all listeners that all cell values in the table's rows may have changed. The number of rows may also have changed and the JTable should redraw the table from scratch. The structure of the table (as in the order of the columns) is assumed to be the same.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
(Jan 25, 2015 09:58 AM)☕⚛Austin⚛☕ Wrote:  This is a Stack Overflow answer:
If you want to notify your JTable about changes of your data, use
tableModel.fireTableDataChanged()

From the documentation:

Notifies all listeners that all cell values in the table's rows may have changed. The number of rows may also have changed and the JTable should redraw the table from scratch. The structure of the table (as in the order of the columns) is assumed to be the same.

Yeah, I already tried that. The problem is the cell is not associated with the objects variable. It's simply copied. So when I update the object, it's not updated in the cell. I think I have to use an AbstractTableModel, but I'm still trying to figure it out..
I haven't worked with JTables a lot, but you could implement the observer design pattern to be notified when your data is changed.

Here's a brief example: http://pastebin.com/uqdmyjv0

Instantiating caller will print
Code
The item has been changed!
The new name is An other item


Possibly Related Threads...
Thread / Author Replies Views Last Post
[Java] by deadsh0tJan 10, 2019 07:30 AM 2 170 Jan 10, 2019 07:30 AM
Last Post: deadsh0t
[Java] by J. McCarthyOct 13, 2017 01:12 PM 0 148 Oct 13, 2017 01:12 PM
Last Post: J. McCarthy
[Java] by mattApr 1, 2017 08:29 PM 2 176 Apr 1, 2017 08:29 PM
Last Post: matt