简体   繁体   English

重新创建Jtable还是删除所有行并添加新行?

[英]Recreate Jtable or remove all rows and add new rows?

I have a JTable and a DefaultTableModel, I want to delete all the rows of JTable every 3 secondes and add new rows to the JTable. 我有一个JTable和一个DefaultTableModel,我想每3秒删除一次JTable的所有行,并向JTable添加新的行。 Considering the performance, What's the best choice to do that, recreate Jtable or remove all rows and add rows? 考虑到性能,重新创建Jtable或删除所有行并添加行的最佳选择是什么?

What's the best choice to do that, renew Jtable or remove all rows and addrows? 这样做,更新Jtable或删除所有行和添加的最佳选择是什么?

If you recreate the JTable, all the renderers and editors will be recreated for the table. 如果重新创建JTable,将为该表重新创建所有渲染器和编辑器。

If you change the TableModel, then the table will need to recreate the TableColumnModel, and all the TableColumns based on the new TableModel. 如果更改TableModel,则该表将需要重新创建TableColumnModel,以及所有基于新TableModel的TableColumns。

There will be many more objects that need to be created to support the JTable/TableModel relationship. 需要创建更多对象来支持JTable / TableModel关系。

Removing rows and adding rows, will just cause the table to repaint the new data using all the current renderers. 删除行并添加行,只会导致表格使用所有当前渲染器重新绘制新数据。 I would say this is more efficient. 我会说这更有效。 And instead of adding rows one at a time, you should add all the rows at once 并且不要一次添加一行,而应该一次添加所有行

You can create a tablemodel that digs into your data object. 您可以创建一个表模型来挖掘您的数据对象。 As soon as you change the underlying data object, you call fireTableDataChanged(). 更改基础数据对象后,立即调用fireTableDataChanged()。

From the JavaDoc for AbstractTableModel#fireTableDataChanged(): 从JavaDoc for AbstractTableModel#fireTableDataChanged():

Notifies all listeners that all cell values in the table's rows may have changed. 通知所有侦听器表行中的所有单元格值可能已更改。 The number of rows may also have changed and the JTable should redraw the table from scratch. 行数也可能已更改,JTable应该从头开始重新绘制表。 The structure of the table (as in the order of the columns) is assumed to be the same. 假定表的结构(按列的顺序)是相同的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM