[英]Removing items from JList
我有一个简单的 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);
}
}
问题是您在侦听器中遇到了问题,因为删除元素后,所选值会发生变化。 这就是您的“valueChanged”方法试图将 selectedValue 置于错误位置的原因。 我看不到你的方法 valueChanged,但我认为这就是原因。
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);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.