使用DefaultListModel从JList中删除元素时出错

我有一个标准的JList ,将在程序运行时更改。 为了让生活更轻松,我创建了一个DefaultListModel并将其分配给JList

 JList CharList = new JList(); DefaultListModel CharListModel = new DefaultListModel(); CharList.setModel(CharListModel); 

我能够将文件加载到列表中,稍后我可以将项添加到列表中,如下所示:

 File ChFile = new File (CharListFile); FileReader freeder = new FileReader (ChFile); BufferedReader breeder = new BufferedReader(freeder); String line; while((line=breeder.readLine())!=null) { int pos = CharList.getModel().getSize(); CharListModel.add(pos, line); } ... ... //and to add items.. int pos = CharList.getModel().getSize(); CharListModel.add(pos, NewCharName); 

但是,我需要能够从列表中删除项目,这给了我一些相当大的麻烦!

我尝试过最明显的方法(是的,选择了一个项目,我已经检索了该索引处的索引和字符串):

 CharListModel.removeElement(CharList.getSelectedValue()); 

但是,这给了我一个’ java.lang.ArrayIndexOutOfBoundsException:-1 ‘错误。

我已经尝试了你在下面的代码中看到的所有排列(有些被注释掉,但你明白了):

 DefaultListModel model = (DefaultListModel) CharList.getModel();//CharListModel; int selectedIndex = CharList.getSelectedIndex(); if (selectedIndex != -1) { //model.remove(selectedIndex); //model.removeElement(CharList.getSelectedValue()); //model.removeElementAt(selectedIndex); } 

以及其他一些排列:

 CharListModel.removeElementAt(CharList.getSelectedIndex()); //or CharListModel.remove(CharList.getSelectedIndex()); //or CharList.remove(SelItemIndex); 

在每种情况下,我都会得到相同的“ ArrayIndexOutOfBoundsException ”错误,即使先前找到了所选索引并没有遇到任何问题。 是的,我知道我刚才说’以前’所以有些东西可能会改变一些东西,但这是在我尝试删除元素之前直接运行的代码:

 int SelItemIndex = CharList.getSelectedIndex(); if(SelItemIndex == -1) { JOptionPane.showMessageDialog(null, "You have to select something!"); return; } String SelItem = CharList.getModel().getElementAt(SelItemIndex).toString(); //Create warning final JComponent[] inputs = new JComponent[] { new JLabel("Bla Bla " + SelItem + " Are you sure?") }; int n = JOptionPane.showConfirmDialog( null, inputs,"Deletion Confirmation Warning", JOptionPane.YES_NO_OPTION); if( n == 1) { //Do not delete return; } 

在尝试删除所选元素之前,这就是全部。

对于我的生活,我不知道为什么这不起作用! 我错过了一些非常愚蠢的东西吗?

令人困惑的更新

JButtonActionPerformed事件中,我使用了这段代码 – 代码中的注释解释了为什么会这么混乱!:

 DefaultListModel CharListModel = (DefaultListModel)CharList.getModel(); if( CharListModel.contains(CharList.getSelectedValue()) == true) { //Selected item is found int selItemIndex = CharListModel.indexOf(CharList.getSelectedValue()); if(selItemIndex != -1) { //Selected item index is NOT -1 and is correct CharListModel.remove(selItemIndex); //Exception in thread "AWT-EventQueue-0" java.lang.ArrayIndexOutOfBoundsException: -1 } else { //NEVER reached JOptionPane.showMessageDialog(null, "OUCH!"); } } 

正如您所看到的,所选项目的索引是正确的,直到删除它为止,然后我再次获得越界exception。 我也在同一个地方试过这个但结果相同:

 CharListModel.removeElement(CharList.getSelectedValue()); 

更加困惑

为了弄清楚发生了什么,我创建了一个新的DefaultListModel ,枚举了旧的,然后将每个名称放入新模型中,除了我要删除的名称(记住我可以获取索引,对象和文字,我只是不能删除它)。

这有效,我现在有一个DefaultListModel其中包含正确的项目,但是,我尝试CharList.setModel(NewModel);的那一刻CharList.setModel(NewModel); 我再一次得到了界限exception。

这让我脱掉了头发! 任何人都可以提供任何想法尝试吗?

各种解决方案

根本不是解决方案,但要解决这个令人抓狂的问题,我正在使用上面列出的方法,在那里我创建列表模型的副本,减去我要删除的项目,然后在使用setModel时捕获exception,因为更新的列表模型被添加到列表就好了,后续的操作,如添加项目等工作正常,直到我尝试再次删除项目!

谢谢你,如果你试图帮助 – 如果你对如何解决这个问题有任何想法,一定要发布!

问候

马克斯

作为参考,我在下面的代码中添加了代码。 如果它没有用,那么更新你的问题可能是一个有用的sscce 。

 panel.add(new JButton(new AbstractAction("Remove") { @Override public void actionPerformed(ActionEvent e) { int index = list.getSelectedIndex(); if (index != -1) { model.remove(index); } } })); 

我有类似的问题。 事实certificate,错误不是因为删除元素,而是显示列表。 实施时

 public void valueChanged(ListSelectionEvent e) 

方法,它更新屏幕上的列表,确保在设置值之前检查模型是否为null。 这就是导致我的例外情况的原因。

创建列表,从那里删除,然后使用列表更新模型也是一种可用的解决方法。

我遇到了同样的问题。 当项目从模型中移除时,它似乎也会从数组中删除。 因此会混淆数组索引。

作为解决方法,我将数组内容移动到List中,并从模​​型中删除List内容。 现在它对我来说很好。

我有同样的问题,我解决这个问题:

按钮动作

 int index = mylist.getSelectedIndex(); MyObject = (MyObject) mylist.getSelectedValue(); int Size = mylistmodel.getSize(); if (index >= 0 && MyObject != null && Size > 0) { modeloLista.removeElementAt(indice); } 

和。

listValueChanged

 if (list.getSelectedValue() != null) { your code.. }