我有一个简单的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)
有什么窍门还是什么?感谢您的帮助。
假设您的索引是非负的(其他人提到的),请参阅(在听众中)是否有效:
((DefaultListModel) jList.getModel()).remove(index);
如果是这样,那么您正在使用陈旧的型号。
根据Javadoc,建议使用remove()
代替removeElementAt()
,因此:
public void actionPerformed(ActionEvent arg0) {
int index = list.getSelectedIndex();
if (index != -1) {
model.remove(index);
}
根据 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);
}
}
问题是您在侦听器中有问题,因为当元素被删除时,所选值将会更改。这就是您的"评估"方法试图使所选值处于错误位置的原因。我看不到您的方法有价值,但我认为这是原因。
DefaultListModel model=new DefaultListModel();
model.clear();
jList1.setModel(model);
如果要删除所有项目
int selectedIndex = yourJLIST.getSelectedIndex();
String [] ListData = new String[yourJLIST.getModel().getSize()];
for (int i = 0; i < ListData.length; i++) {
if(i == selectedIndex){
}else{
ListData[i] = yourJLIST.getModel().getElementAt(i);
}
}
yourJLIST.setListData(ListData);