soupdiver
soupdiver

Reputation: 3683

auto update jtable columns when model changes

I've got a class which implements the TableModel Interface. When I call setModel on my jTable and set my class as the model and then call jTable.updateUI();everything is fine. But in some circumstances I want to change the model with a different structure. Displaying the data still works fine but my columns are not updated. Is there a way of forcing my table to also refresh the columns from new model?

Upvotes: 2

Views: 6654

Answers (4)

vvinjj
vvinjj

Reputation: 83

Try this (insert after setModel(myTableModel); line):

myJTable.createDefaultColumnsFromModel();

In my case it solved the update issue, while: .repaint(), .invalidate(), .fireTableDataChanged() didn't help.

Upvotes: -1

lut
lut

Reputation: 1

If like me you are just writing a small hack, you may consider using the .repaint() method, instead of the .updateUI() method.

I used this because I don't use a TableModel, but I just have data in a Object[][], just like in the first example of the sun (well oracle) tutorial

Upvotes: -1

mKorbel
mKorbel

Reputation: 109823

  • jTable.updateUI(); is Look and Feel relevant method, don't use that

I've got a class which implements the TableModel Interface.

  • you have to override right notifiers for methods from TableModel

  • use DefaultTableModel, there are all notifiers implemented in the API,

  • all updates must be done on EventDispatchThread

Upvotes: 4

Puce
Puce

Reputation: 38152

The model needs to fire the according event, e.g.: fireTableStructureChanged, if both the data and the structure changed.

http://docs.oracle.com/javase/tutorial/uiswing/components/table.html#fire

Upvotes: 6

Related Questions