tags:

views:

35

answers:

3

I would like to display a table of incoming packets and their attributes. Does JTable allow me to add rows dynamically? The only way I have been able to update the view is to construct a new table object.

+1  A: 

yes you can manipulate the underlying model of the JTable. The model is where the data is kept and it gets queried by the JTable for the different cell contents. You can manipulate the model either by using JTable.setModel(...) or by implementing your own TableModel and manipulating it's data directly which is a fairly easy task with lots of tutorials on the web. You just have to make sure that the JTable gets updated when you change the model, which is easyily achieved by a JComponent.pack() or similar methods.

check here for some tutorials:

http://download-llnw.oracle.com/javase/tutorial/uiswing/components/table.html

http://www.javalobby.org/articles/jtable/

http://www.java2s.com/Code/Java/Swing-JFC/TableModelExample.htm

http://www.codetoad.com/java_JTable.asp

hope that helped...

smeg4brains
+1  A: 

You can use a TableModel (e.g. derive from AbstractTabelModel) and then use methods such as fireTableRowsInserted to tell the table the underlying data has changed.

Emile
+1  A: 

Use the DefaultTableModel. It has methods like addRow(...), removeRow(...) that supports dynamic updating of the TableModel.

You can always look at the source code of this class for the proper way to use the fireXXX methods if you need to use a custom TableModel.

camickr