自定义类和 JList
我正在 NetBeans 中从事 Java 项目。
我需要在 JList
的 ArrayList
中显示每个 State
(例如“Oklahoma”)。
我不知道如何做到这一点......尤其是在 NetBeans 中。
我认为它涉及创建一个 DefaultListModel
,其中每个 State
作为 String
。我已经尝试过一百万种不同的方法,但都无济于事。
是否可以仅加载 ArrayList
(也许如果 State
类具有 toString()
方法)?这将特别有用,因为我可以通过 actionPerformed()
直接修改 ArrayList
。
为了解决这个问题,我们假设 State
是一个 name
类型为 String
和 population
的对象> 类型为int
。
我希望这是有道理的。
谢谢。
I'm working in NetBeans on a Java Project.
I need to display every State
(e.g. "Oklahoma") in an ArrayList<State>
in a JList
.
I can't figure out how to do this... especially not in NetBeans.
I think it involves creating a DefaultListModel
with each State
as a String
. I've tried this a million different ways to no avail.
Is it possible to just load the ArrayList<State>
(perhaps if the State
class has a toString()
method)? This would be especially helpful, because I could directly modify the ArrayList<State>
via actionPerformed()
.
For the sake of the question, let's assume a State
is an object with a name
of type String
and a population
of type int
.
I hope this makes sense.
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
所有这些答案都很棒,而且当然有用。我最终从控制器中非常简单地解决了这个问题:
view.getStatesList().setListData(model.getStates().toArray());
关键是使用 .setListData() (可以将数组作为参数)而不是
.setModel()
。All these answers are great, and certainly useful. I ended up solving it very simply from the controller:
view.getStatesList().setListData(model.getStates().toArray());
The key is the use of
.setListData()
(which can take an Array as an argument) instead of.setModel()
.不幸的是,使用 List 的 JList 唯一“简单”初始化是通过 Vector
Unfortunately, the only 'easy' initialization of JList with a List is via Vector
最简单的解决方案是创建一个 DefaultListModel,然后迭代 ArrayList,将 State 对象添加到列表模型中。然后使用这个模型作为您的 JList 的模型。另一方面,如果您想在模型中使用 ArrayList,那么您可能需要创建一个扩展 AbstractListModel 类的类,但要准备好充实该类的更多方法才能使其正常工作。
The easiest solution is to create a DefaultListModel and then iterate through the ArrayList adding your State objects into the list model as you go. Then use this model as the model of your JList. If on the other hand you want to use the ArrayList in the model, then you'll likely need to create a class that extends AbstractListModel class, but be ready to have to flesh out more of the methods of this class for it to work well.
这里提供的所有答案都很棒(+1)。在我看来,您最好创建自己的模型,稍后您可以轻松地重用该模型,了解它的工作原理。
这里有一个非常简单的使用示例。
我建议您始终在某个地方(例如在实用程序包中)提供这样的模型,可供使用。您会发现这种简单的模型经常有用。它非常适合显示任何类型的数据。
祝一切顺利,博罗。
All answers provided here are great (+1). In my opinion you will be best creating your own model which you can later reuse easily knowing how it works.
And here you have a very simple use example.
I recommend it for you to have such a model always somewhere, e.g. in a utility package, available and ready to use. You will see how often such simple model you will find useful. This one is perfect for displaying any type of data.
All the best, Boro.
我猜你在这里谈论的是 Matisse GUI 构建器,否则这应该是相当微不足道的。
首先,您需要
在调用 initComponents() 之前在类的构造函数中创建自定义 ListModel,初始化 ListModel,并使其成为字段而不是局部变量。
在 NetBeans GUI 构建器中,右键单击 JLIst,然后选择“自定义代码”。
您将看到类似于 jList1.setModel(...) 的内容 将下拉列表更改为自定义属性并编辑代码以读取
I presume your talking about the Matisse GUI builder here, otherwise this should be fairly trivial.
First off you need to create the custom ListModel
in the constructor of your class initialise the ListModel before initComponents() is called, and make it a field not a local variable.
In the NetBeans GUI builder right click on your JLIst, and select 'customize code'.
You will see something along the lines of jList1.setModel(...) Change the dropdown to custom property and edit the code to read