tags:

views:

1367

answers:

1

I have a JTable created from Vector. How can the JTable be refreshed to display new data that is added to the Vector?

+3  A: 

Your JTable should update automatically when a change to the TableModel happens. I'm taking a leap here but I'm guessing that you're not using your own TableModel and just called the JTable constructor with your Vector. In this case you can get a hook on the TableModel and cast it to a DefaultTableModel and then call one its notification methods to let the JTable know of a change, something like:

DefaultTableModel model = (DefaultTableModel)table.getModel();
model.fireTableChanged(new TableModelEvent(........));

What I would really recommend is using your own TableModel unless this is something very trivial, but the fact you're updating the data indicates it's not.

Check out the sun tutorial on working with tables, inparticular the section on listening for data changes.

It might seem like more work up front, but it will save you alot of headaches in the long run and it The Right Way to do it

MrWiggles