从JList删除选定的项目 [英] Delete selected item from JList
问题描述
有人可以告诉我从我的JList
删除所选项目的简短方法吗?
Can anyone tell me a short way to delete the selected items from my JList
?
我在google和此处进行了搜索,但发现了很多方法.我应该使用哪种方式?
I searched on google and here, but I found very many ways. Which way should I use?
推荐答案
正如@Andreas_D所说,以数据为中心,更抽象的ListModel是解决方案.这可以是 DefaultListModel .您应该在JList中显式设置模型. 因此(感谢@kleopatra发表评论):
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):
DefaultListModel model = (DefaultListModel) jlist.getModel();
int selectedIndex = jlist.getSelectedIndex();
if (selectedIndex != -1) {
model.remove(selectedIndex);
}
DefaultListModel中有几种remove...
方法.
顺便说一句,这是一个好问题,因为API(ListModel)中没有即时解决方案.
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删除选定的项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!