从JList中删除项目

问题描述 投票:2回答:5

我有一个简单的Jlist包含来自List<String>的数据,现在我想从Jlist中删除所选项目。这是代码:

final DefaultListModel<String> model = new DefaultListModel();
final JList list = new JList(model);

//filling list
//loop for every element from List<String>
 public static void sample(DefaultListModel model, List<String> data)
      for(int i=;i<data.size();i++)
        {model.addElement(data.get(i));}

//btn pressed
public void actionPerformed(ActionEvent arg0) {
    int index = list.getSelectedIndex();
    model.removeElementAt(index);
}

我收到此错误:

Exception in thread "AWT-EventQueue-0" java.lang.ArrayIndexOutOfBoundsException: -1
at java.util.ArrayList.elementData(Unknown Source)
at java.util.ArrayList.get(Unknown Source)
at javax.swing.JList.fireSelectionValueChanged(Unknown Source)
at javax.swing.JList$ListSelectionHandler.valueChanged(Unknown Source)
at javax.swing.DefaultListSelectionModel.fireValueChanged(Unknown Source)
at javax.swing.DefaultListSelectionModel.fireValueChanged(Unknown Source)
at javax.swing.DefaultListSelectionModel.fireValueChanged(Unknown Source)
at javax.swing.DefaultListSelectionModel.removeIndexInterval(Unknown Source)
at javax.swing.plaf.basic.BasicListUI$Handler.intervalRemoved(Unknown Source)
at javax.swing.AbstractListModel.fireIntervalRemoved(Unknown Source)
at javax.swing.DefaultListModel.remove(Unknown Source)
at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
at java.awt.Component.processMouseEvent(Unknown Source)
at javax.swing.JComponent.processMouseEvent(Unknown Source)
at java.awt.Component.processEvent(Unknown Source)
at java.awt.Container.processEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Window.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$000(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)

是诀窍还是什么?感谢帮助。

java swing jlist defaultlistmodel
5个回答
5
投票

假设你的索引是非负的(正如其他人所提到的),看看它是否有效(在你的监听器中):

((DefaultListModel) jList.getModel()).remove(index);

如果是这样,那么您正在使用过时的模型。


2
投票

根据getSelectedIndex()的Javadoc:

返回最小的选定单元格索引;在列表中仅选择单个项目时的选择。选择多个项目时,它只是选择的最小索引。如果没有选择,则返回-1

您遇到错误的原因是由于某种原因,您的列表中没有选择任何项目,因此此方法返回-1。当你调用removeElementAt()并将其作为参数值传递-1时,它会抛出异常。

你需要做的是如下:

public void actionPerformed(ActionEvent arg0) {
    int index = list.getSelectedIndex();
    if(index >= 0){ //Remove only if a particular item is selected
        model.removeElementAt(index);
    }
}

2
投票

根据javadoc,建议使用remove()而不是removeElementAt(),因此:

public void actionPerformed(ActionEvent arg0) {
    int index = list.getSelectedIndex();
    if (index != -1) {
        model.remove(index);
}

1
投票

问题是您在侦听器中遇到问题,因为删除该元素时,所选值将更改。这就是你的“valueChanged”方法试图将selectedValue置于错误位置的原因。我看不到你的方法valueChanged,但我认为这就是原因。


1
投票
DefaultListModel model=new DefaultListModel();
    model.clear();
 jList1.setModel(model);

如果你想删除所有项目

© www.soinside.com 2019 - 2024. All rights reserved.