java 使用 ListModel 作为模型类型从 JList 中删除项目
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5775036/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Removing an item from the JList using ListModel as model type
提问by Deepak
I have the JList
which is using ListModel
and not the DefaultListModel. I don't want to change the type now because I am using this in many places. I want to remove a selected item from the same list. How do i do this? I am using the following code but its not working for me.
我有JList
正在使用的ListModel
而不是 DefaultListModel。我现在不想改变类型,因为我在很多地方都在使用它。我想从同一个列表中删除一个选定的项目。我该怎么做呢?我正在使用以下代码,但它对我不起作用。
made_list.removeSelectionInterval(
made_list.getSelectedIndex(), made_list.getSelectedIndex());
--EDIT--
- 编辑 -
I am using the following code when I create my list:
我在创建列表时使用以下代码:
made_list = new javax.swing.JList();
made_list.setModel(new DefaultListModel());
And then in the JButton
mouseclick event, I am using the following code to remove the selected item from the list when the button is pressed
然后在JButton
mouseclick 事件中,我使用以下代码在按下按钮时从列表中删除所选项目
private void removeActionPerformed(java.awt.event.ActionEvent evt) {
//made_list.removeSelectionInterval(made_list.getSelectedIndex(),
//made_list.getSelectedIndex());
System.out.println(made_list.getModel());
DefaultListModel model = (DefaultListModel)made_list.getModel();
model.remove(1);
}
回答by Hovercraft Full Of Eels
removeSelectionInterval removes nothing from the model or the list except the selection interval. The list items remain unscathed. I'm afraid that you're either going to have to extend your ListModel and give it a removeItem(...) method as well as listeners and the ability to fire notifiers, etc... a la AbstractListModel -- quite a lot of work! If it were my money, though, I'd go the easy route and simply use a DefaultListModel for my model as it is a lot safer to do it this way, a lot easier, and will take a lot less time. I know you state that you don't want to use these, but I think you'll find it a lot easier than your potential alternatives.
removeSelectionInterval 除了选择间隔之外,不会从模型或列表中删除任何内容。列表项保持完好。恐怕你要么不得不扩展你的 ListModel 并给它一个 removeItem(...) 方法以及侦听器和触发通知程序的能力等等...... a la AbstractListModel - 相当多工作的!不过,如果这是我的钱,我会走简单的路线,简单地为我的模型使用 DefaultListModel,因为这样做更安全,更容易,而且会花费更少的时间。我知道你说你不想使用这些,但我认为你会发现它比你的潜在替代方案容易得多。
An example of an SSCCEis something like this:
SSCCE 的一个例子是这样的:
import java.awt.event.*;
import javax.swing.*;
public class Foo1 {
private String[] elements = {"Monday", "Tueday", "Wednesday"};
private javax.swing.JList made_list = new javax.swing.JList();
public Foo1() {
made_list.setModel(new DefaultListModel());
for (String element : elements) {
((DefaultListModel) made_list.getModel()).addElement(element);
}
JButton removeItemBtn = new JButton("Remove Item");
removeItemBtn.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
removeActionPerformed(e);
}
});
JPanel panel = new JPanel();
panel.add(new JScrollPane(made_list));
panel.add(removeItemBtn);
JOptionPane.showMessageDialog(null, panel);
}
private void removeActionPerformed(ActionEvent e) {
System.out.println("made_list's model: " + made_list.getModel());
System.out.println("Model from a fresh JList: " + new JList().getModel());
DefaultListModel model = (DefaultListModel) made_list.getModel();
if (model.size() > 0) {
model.remove(0);
}
}
public static void main(String[] args) {
new Foo1();
}
}
回答by camickr
You've been given a link to different sections of the Swing tutorial in the past to help solve problems. This was done for a reason. It helps solve your current problem. It gives you a reference for future problems.
过去,您已经获得了指向 Swing 教程不同部分的链接,以帮助解决问题。这样做是有原因的。它有助于解决您当前的问题。它为您以后的问题提供了参考。
All you need to do is look at the Table of Contents for the Swing tutorial and you will find a section on "How to Use Lists" which has a working example that adds/removes items from a list. Please read the tutorial first.
您需要做的就是查看 Swing 教程的目录,您会找到关于“如何使用列表”的部分,其中有一个从列表中添加/删除项目的工作示例。请先阅读教程。
Or if you can't remember how to find the Swing tutorial then read the JList API where you will find a link to the same tutorial.
或者,如果您不记得如何找到 Swing 教程,请阅读 JList API,您将在其中找到指向同一教程的链接。
回答by nusry
//First added item into the list
DefaultListModel dlm1=new DefaultListModel();
listLeft.setModel(dlm1);
dlm1.addElement("A");
dlm1.addElement("B");
dlm1.addElement("C");
// Removeing element from list
Object[] temp=listRight.getSelectedValues();
if(temp.length>0)
{
for(int i=0;i<temp.length;i++)
{
dlm1.removeElement(temp[i]);
}
}