使用DefaultListModel从JList中删除元素时出错 [英] Error removing element from JList with DefaultListModel
问题描述
我有一个标准的 JList
,它将在程序运行时更改。为了让生活更轻松,我创建了一个 DefaultListModel
并将其分配给 JList
:
I have a standard JList
that will be changed while the program is running. In order to make life easier I have created a DefaultListModel
and assigned it to the JList
:
JList CharList = new JList();
DefaultListModel CharListModel = new DefaultListModel();
CharList.setModel(CharListModel);
我可以将文件加载到列表中,稍后我可以将项目添加到列表中这个:
I am able to load a file into the list, and later I can add items to the list like this:
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);
但是,我需要能够从列表中删除项目,这给了我一些可观的麻烦!
However, I need to be able to remove items from the list, and this is giving me some considerable trouble!
我尝试过最明显的方法(是的,选择了一个项目,我已经检索了索引和该索引处的字符串):
I have tried the most obvious way (Yes an item is selected, and I have already retrieved both the index and the string at that index):
CharListModel.removeElement(CharList.getSelectedValue());
然而,这给了我一个' java.lang.ArrayIndexOutOfBoundsException:-1 '错误。
However, this gives me a 'java.lang.ArrayIndexOutOfBoundsException: -1' error.
我已经尝试了你在下面的代码中看到的所有排列(有些被注释掉但是你明白了):
I have tried all of the permutations that you can see in the code below (Some are commented out but you get the idea):
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 '错误,即使之前找到的所选索引没有任何问题。是的,我知道我刚才说'以前'所以有些东西可能会改变一些东西,但这里是我试图删除元素之前直接运行的代码:
In each case I get the same 'ArrayIndexOutOfBoundsException' error, even though the selected index is previously found with no trouble. And yes, I know I just said 'previously' so something could have changed things, but here is the code that runs directly before I try to remove the element:
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("<html>Bla Bla " + SelItem + " Are you sure?</html>")
};
int n = JOptionPane.showConfirmDialog( null, inputs,"Deletion Confirmation Warning", JOptionPane.YES_NO_OPTION);
if( n == 1)
{
//Do not delete
return;
}
在尝试删除所选元素之前,这就是全部。
That is all there is before trying to remove the selected element.
对于我的生活,我不知道为什么这不起作用!我错过了一些非常愚蠢的东西吗?
For the life of me I don't know why this is not working! Am I missing something really silly here?
在 ActionPerformed中
JButton的事件
我使用过这段代码 - 代码中的注释解释了为什么会这么混乱!:
In the ActionPerformed
event of a JButton
I have used this code - The comments in the code explain why this is so confusing!:
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!");
}
}
如您所见,所选项目的索引是正确的,直到删除它,然后我再次获得越界异常。我也在同一个地方尝试了这个但结果相同:
As you can see, the index of the selected item is correct right up to the point of removing it, whereupon I once again get the out of bounds exception. I also tried this in the same place but with the same results:
CharListModel.removeElement(CharList.getSelectedValue());
更加混乱
尝试为了弄清楚发生了什么,我创建了一个新的 DefaultListModel
,枚举旧的,并将每个名称放入新模型中,除了我要删除的名称(记住我可以得到索引,对象和文本,我只是不能删除它。)
Even more confusion
In an attempt to work out what is going on I have created a new DefaultListModel
, enumerated the old one, and put each name into the new model, except the one that I want to remove (Remember I can get the index, the object and the text, I just cant delete it).
这已经奏效了,我现在有一个 DefaultListModel
中包含正确的项目,但是,我尝试 CharList.setModel(NewModel);
的那一刻我再次获得了界限例外。
This has worked and I now have a DefaultListModel
with the correct items in it, however, the moment that I try to CharList.setModel(NewModel);
I once again get the out of bounds exception.
这让我脱掉了头发!任何人都可以提供任何想法尝试吗?
This has got me pulling out hair! Can anyone offer any ideas to try?
根本不是一个解决方案,但要解决这个令人抓狂的问题我是使用上面列出的方法,我在其中创建列表模型的副本,减去我要删除的项目,然后在使用 setModel
时简单地捕获异常,因为更新后的列表模型很好地添加到列表中,后续操作(如添加项目等)工作正常,直到我尝试再次删除项目为止!
Not really a resolution at all, but to work around this maddening problem I am using the method laid out above, where I create a copy of the list model, minus the item that I want to delete and then simply catch the exception when using setModel
, since the updated list model is added to the list just fine, and subsequent actions such as adding items etc. work okay, right up until I try to delete an item again anyway!
谢谢,如果你试图提供帮助 - 如果你对如何解决这个问题有任何想法,请务必发布!
Thanks if you tried to help - and if you have any ideas about how to hunt down this problem, by all means post away!
问候
最大
推荐答案
作为参考,我将以下代码添加到此示例的。如果它没有帮助,更新您的问题可能是一个有用的 sscce 。
For reference, I added the code below to this example. If it's not helpful, it may be a useful sscce for updating your question.
panel.add(new JButton(new AbstractAction("Remove") {
@Override
public void actionPerformed(ActionEvent e) {
int index = list.getSelectedIndex();
if (index != -1) {
model.remove(index);
}
}
}));
这篇关于使用DefaultListModel从JList中删除元素时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!