开发者

how to limit the size of a JTable and its internal table model

I have a JTable using DefaultTableModel as its internal data model. It will receive packet from network and show the packet in the JTable. Now i want to limit data model size so that it will only contain the newest packets and drop of the oldest, but the DefaultTableModel uses a dataVector of type Vector which ha开发者_StackOverflow社区s no size limit. Could anybody please give some help? Thanks!


If you always insert new rows at the top of the table, you could easily perform a check on the TableModel when you do so, and remove manually:

 model.insertRow(0,rowData);
 while (model.getRowCount() > myMaxRowCount) {
    model.removeRow(model.getRowCount()-1);
 }

Another option would be to put this process into an extension of DefaultTableModel (or even AbstractTableModel, which would allow you to ditch the Vector for something a bit more modern). The Model could hold the maxRowCount that you want to maintain and then you can implement a new updateModel method that will do the add of the new data and the remove of the old.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜