0

so the scenario is that I've got a JTable with a number of JComboBox's as cells. On the selection of an element of a JComboBox, there needs to be a change in the structure of the Table Model. I've also got an 'output table' below which listens to the selection of the JComboBox's and re-validates accordingly, because of this, I need to keep the model of the query table the same so that it can reuse the listener. How can I change the structure of the Table Model?

DefaultTableModel QueryTableModel = new DefaultTableModel(dropDownUserSelection, resultsListHeadings );     
queryTable.setModel(QueryTableModel);

JComboBox box = new JComboBox(boxModel);      
queryTable.getColumnModel().getColumn(i).setCellEditor(new DefaultCellEditor(box));

I apologize if I am asking a question which has already been asked elsewhere, but I've had a poke around and couldn't find what I was looking for.

Thanks

J Smith
  • 3,105
  • 4
  • 19
  • 35
  • That's a little unclear. By structure, I assume you want to add/remove columns? – MadProgrammer Feb 13 '13 at 23:46
  • Hi, thanks for your response... Yes that's correct. – J Smith Feb 13 '13 at 23:50
  • 1
    Please edit your question to include an [sscce](http://sscce.org/) that shows your combo, for [example](http://stackoverflow.com/a/7356518/230513), and your call `fireTableStructureChanged()`. – trashgod Feb 13 '13 at 23:58

1 Answers1

2

The TableModel has the responsibility for notifying the parent table (or anybody listening) of changes to the model.

The general events available are data changed, cell updated, row inserted/removed and structure changed.

The "structure changed" tells the parent table that the structure of the table model (the number of columns and/or column names and/or types has changed) and it should completely update itself.

There are a number of ways you could achieve this. You could have the existing table model change it self accordingly and fire a "structure changed" event or you could construct a new table model and apply it to the JTable, depending on your needs.

MadProgrammer
  • 336,120
  • 22
  • 219
  • 344
  • 1
    +1 `setModel()`, seen [here](http://stackoverflow.com/a/8260663/230513), also fires the required event. – trashgod Feb 14 '13 at 00:02
  • @Eng.Fouad 40 today, 40 today...I feel so old ;) – MadProgrammer Feb 14 '13 at 00:35
  • If I do setModel(), then the model changes, and the listener no longer listens because it's a different model. So is the only option to fire structure changed? – J Smith Feb 14 '13 at 10:55
  • 1
    @JSmith _is the only option to fire structure changed_ no, use the DefaultTableModel api to update (f.i. setDataVector) and the model will fire as appropriate. Anyway, your setup smells fishy - best to show an SSCCE as already suggested. And while you are at it: please learn java naming conventions and stick to them :-) – kleopatra Feb 14 '13 at 14:29
  • Hi, your suggestion of setDataVector() worked a treat. Next time I will be more helpful by including an SSCCE and sticking to naming conventions. Thanks! – J Smith Feb 14 '13 at 18:32