从JList删除选定的项目 [英] Delete selected item from JList

查看:77
本文介绍了从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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆