tags:

views:

176

answers:

4

Good evening

I have a JTable that I built with a TableModel how to update the elements of the table, because when I do table = new JTable (new TableProg (elementTab)) I create another table above the original table and it is very ugly

So for example how to update element of table in a loop at each iteration as "elementTab" changes?

thank you very much

+1  A: 

I recommend you to extend an AbstractTableModel and implement an void addRow(YourObject row) that fits you. Or if you want to update the hole tables data, you could implement an void addElements(YourCollection elements) and use the void fireTableDataChanged()-method.

I.e. keep your data in a LinkedList and don't forget to use void fireTableRowsInserted(int firstRow, int lastRow) when you add a new row.

Jonas
yes i use a AbstractTableModel, and i dont want add row i want update the whol elements of the table
tuxou
@tuxou: Ah, I see. You can use `fireTableDataChanged()` instead of `fireTableRowsInserted` then. I updated my answer.
Jonas
+1  A: 

Not sure I understand your question.

To update the cells of a table you just use

table.setValueAt(...);

To update the entire table at once you can create a new TableModel and then update the table with the new model:

TableModel model = new YourTableModel(...);
table.setModel( model );

While learning how to use models start with the DefaultTableModel since it also support dynamic changes to the model by using addRow(...) and removeRow().

camickr
A: 

A little more details on what elementTab is would be very helpful in suggesing a solution. If you want to add rows, simply create an array of objects and add to the model. Eg.

    TableModel model = table.getModel();
Object[] row = new Object[] {1,2,3};
model.addRow(row);

if you want to selective update cells, use model.setValueAt(Object,row,col);

vinny
-1, Firstly already gave the method to update individual cells, there is no need to repeat suggestions. Secondly, the addRow() method with not since TableModel does not support that method. You need to make sure you are using a model (like DefaultTableModel as I also suggested) that supports that method.
camickr
i use AbstractTableModel
tuxou
A: 

Thanks you so much everyone i solved my probleme with :

model = new TableProg(elements); table.setModel(model);

in the loop

tuxou