开发者

Adding Entries to a JTable

I would like to display a table of incoming packets and their attributes. Does JTab开发者_C百科le allow me to add rows dynamically? The only way I have been able to update the view is to construct a new table object.


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...


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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜