使用 JList .setModel() 方法并将类作为参数

发布于 2024-10-03 10:55:35 字数 721 浏览 0 评论 0原文

我的最终目标是拥有一个在运行时刷新其内容的 JList,并且我找到了一个适用于这篇文章的解决方案 这里是这样,但是我很好奇为什么我最初的想法没有。

截至目前,我有类似的设置并且它有效:

DefaultListModel default = new DefaultListModel();

for(int i = 0; i < array.size() ; ++i){
   test.addElement(array.get(i));
}
list.setModel(default);

以下是我最初的计划。我想要一个实现 ListModel 的类作为参数传递,希望它能刷新 JList。

SomeClass test = new SomeClass(); //Implements ListModel
list.setModel(test);

或者

SomeClass test = new SomeClass(); //Implements ListModel
list = new JList(test);

这些都不起作用,这让我很困惑。最后两个方法可以工作一些吗,代码干净多了。

谢谢。

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.

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.

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

橘香 2024-10-10 10:55:35

如果正确实现 ListModel,第一种方法应该有效。关键是,当您更改数据时,您需要调用:

fireContentsChanged(...);

来自 AbstractListModel (我假设您扩展了它)。调用此方法将告诉 JList 重新绘制自身。

第二种方法不起作用,因为您只是创建了一个位于内存中的新 JList 组件。创建组件不会将其添加到 GUI。因此,如果您使用此方法,您需要从 GUI 中删除原始 JList,然后将新 JList 添加到 GUI。这不是很方便,也是不应该使用这种方法的一个很好的理由。设置模型始终是首选方法。

The first approach should work if you implement the ListModel correctly. The key is that when you change the data you need to invoke:

fireContentsChanged(...);

from the AbstractListModel (which I assume you extend). Invoking this method will tell the JList to repaint itself.

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.

多情癖 2024-10-10 10:55:35

在我看来,第一种情况似乎是一个解决方案。你能提供一个可测试的例子吗?

第二种情况不起作用,因为您只是重用变量,而实际上并没有更改 Gui 上的 JList。我假设您已经将此列表添加到代码中前面的那个人中。

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.

无敌元气妹 2024-10-10 10:55:35

您的 ListModel 实现很可能是错误的。

Most likely your implementation of ListModel is wrong.

樱娆 2024-10-10 10:55:35

为什么不使用:

DefaultListModel<String> lstList = new DefaultListModel<String>();

Why not using:

DefaultListModel<String> lstList = new DefaultListModel<String>();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文