java 使用 JList .setModel() 方法和一个类作为参数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4231820/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Using the JList .setModel() method with a class as an argument
提问by Koop
My ultimate goal is to have a JList which refreshes its content at runtime, and I have found a solution that works from this post here on SO, however I am curious why my original idea did not.
我的最终目标是拥有一个在运行时刷新其内容的 JList,并且我在 SO 上的这篇文章中找到了一个有效的解决方案,但是我很好奇为什么我最初的想法没有。
As of now, I have something like this setup and it works:
到目前为止,我有类似这样的设置并且它有效:
DefaultListModel default = new DefaultListModel();
for(int i = 0; i < array.size() ; ++i){
test.addElement(array.get(i));
}
list.setModel(default);
Below was my original plan. I wanted to have a class which implemented ListModel be passed as an argument, hoping it would refresh the JList.
下面是我原来的计划。我想让一个实现 ListModel 的类作为参数传递,希望它能刷新 JList。
SomeClass test = new SomeClass(); //Implements ListModel
list.setModel(test);
or
或者
SomeClass test = new SomeClass(); //Implements ListModel
list = new JList(test);
Neither of these work, which confuses me. Could these last two methods work some how, the code is so much cleaner.
这些都不起作用,这让我很困惑。后两种方法能否以某种方式工作,代码更加简洁。
Thanks.
谢谢。
采纳答案by camickr
The first approach should work if you implement the ListModel correctly. The key is that when you change the data you need to invoke:
如果您正确实现 ListModel,第一种方法应该有效。关键是当你更改数据时需要调用:
fireContentsChanged(...);
from the AbstractListModel (which I assume you extend). Invoking this method will tell the JList to repaint itself.
来自 AbstractListModel (我假设你扩展了它)。调用此方法将告诉 JList 重新绘制自身。
The second approach won't work because you just create a new JList component that is sitting in memory. Creating the component does not add it to the GUI. So if you use this approach you need to remove the original JList from the GUI and then add your new JList to the GUI. This is not very convenient and is a good reason why this approach should not be used. Setting the model is always the preferred approach.
第二种方法不起作用,因为您只是创建了一个位于内存中的新 JList 组件。创建组件不会将其添加到 GUI。因此,如果您使用这种方法,您需要从 GUI 中删除原始 JList,然后将您的新 JList 添加到 GUI。这不是很方便,也是不应该使用这种方法的一个很好的理由。设置模型始终是首选方法。
回答by jzd
The first case seems like a solution in my mind. Can you provide a testable example?
第一种情况在我看来是一个解决方案。你能提供一个可测试的例子吗?
The second case will not work because you are just reusing a variable and are not actually changing the JList on the Gui. I am assuming you already added this list to the guy earlier in code.
第二种情况将不起作用,因为您只是重用了一个变量,而实际上并未更改 Gui 上的 JList。我假设您已经在代码中将这个列表添加到了前面的那个人。
回答by milan
Most likely your implementation of ListModel is wrong.
很可能您对 ListModel 的实现是错误的。
回答by Trevor Dndreezy Netshisaulu
Why not using:
为什么不使用:
DefaultListModel<String> lstList = new DefaultListModel<String>();