从 JList 中删除所选项目
谁能告诉我一种从 JList
中删除所选项目的快捷方法?
我在谷歌和这里搜索,但我发现了很多方法。我应该使用哪种方式?
Can anyone tell me a short way to delete the selected items from my JList
?
I searched on google and here, but I found very many ways. Which way should I use?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
正如 @Andreas_D 所说,以数据为中心、更抽象的 ListModel 是解决方案。这可以是 DefaultListModel。您应该在 JList 中显式设置模型。
所以(感谢 @kleopatra 的评论):
DefaultListModel 中有几个
remove...
方法。顺便说一句,这是一个很好的问题,因为 API(ListModel)中没有立即的解决方案。
As @Andreas_D said, the data centered, more abstract ListModel is the solution. This can be a DefaultListModel. You should explicitly set the model in the JList.
So (thanks to comment by @kleopatra):
There are several
remove...
methods in DefaultListModel.By the way, this is a good question, as there is no immediate solution in the API (ListModel).
JList
组件由列表模型支持。因此,从列表视图中删除项目的唯一推荐方法是将其从模型中删除(并刷新视图)。The
JList
component is backed by a list model. So the only recommended way to remove an item from the list view is to delete it from the model (and refresh the view).从模型中删除该元素后,它也将从列表中删除。您可以参考这篇 JList 文章了解更多信息。由于列表由模型支持,因此如果您对模型执行任何操作,它也会反映在列表中。您只需要刷新视图即可。
Once you delete the element from the model it will also be removed from the list. You can refer this JList article for more information. As the list is backed by a model if you do any operation on the model it will also reflect on the list. you just need to refresh the view.