views:

18

answers:

1

For a hw assignment, I need to setup a JTable in Swing, and populate the table's contents via a custom model class that extends the AbstractTableModel. My questions regarding this overly complicated process are many fold, but I'll try and keep it simple.

My first step is to get data into the model right? When I was using a JList (I could use the DefaultListModel) it was as simple as list.addElement(). I am finding, however, that adding data to my JTable model isn't as easy. The default methods for the AbstractTableModel consist of get row/column numbers, and that is it. In order to get data into my model, do I need to write my own addElement model? Any pointers on doing that?

After I get data into my model, is it just a matter of table.setModel(myModel);? What do I need to do to handle the index of the columns and rows?

Any help or insight on this matter would really go along way, I am just getting near the point of throwing my computer out the window...

A: 

You need to override the empty AbstractTableModel#setValueAt.

AbstractTableModel doesn't have any data field, so you're free to use anything that suits you.

Your table model doesn't need to worry about model/view index conversion (if that's what you're asking about), because JTable#setValue does it for you.

(NOT an answer: If you just want to get over with it and your teacher has enough lax, rename DefaultTableModel ;)

Geoffrey Zheng