简体   繁体   English

删除和添加JList中的项目吨

[英]Removing And Adding Tons of Items In JList

在此处输入图片说明

The purpose is moving selected items from left to right and vice versa. 目的是将选定的项目从左向右移动,反之亦然。
With few items works fine, but once it deals with many items like over 20k, it goes so slow. 很少有项目可以正常工作,但是一旦处理了超过20k的许多项目,它就会变得如此缓慢。

DefaultListModel<String> fromModel = (DefaultListModel<String>) fromJList.getModel();
DefaultListModel<String> toModel = (DefaultListModel<String>) toJList.getModel();

int selectedIndex;
while ((selectedIndex = fromJList.getSelectedIndex()) != -1)
{
     String itemToRemove = fromModel.remove(selectedIndex);
     fromList.remove(itemToRemove);
     toList.add(itemToRemove);
     toModel.addElement(itemToRemove);
}

DefaultListModel's implementation is not the best for this kind of job. DefaultListModel的实现对于这种工作不是最好的。 And adding/removing one by one is not efficient too, as it fires refresh event at each call. 而且一个接一个地添加/删除也不是很有效,因为它会在每次调用时触发刷新事件。

Best would be to create your own ListModel by extending AbstractListModel. 最好是通过扩展AbstractListModel来创建自己的ListModel。

https://docs.oracle.com/javase/8/docs/api/javax/swing/AbstractListModel.html https://docs.oracle.com/javase/8/docs/api/javax/swing/AbstractListModel.html

The idea is to do all the updates, add/remove items and then call fireContentsChanged 这个想法是进行所有更新,添加/删除项目,然后调用fireContentsChanged

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

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