自定义类和 JLists [英] Custom Classes and JLists
问题描述
我在 NetBeans 中从事 Java 项目.
I'm working in NetBeans on a Java Project.
我需要在 JList
中的 ArrayList
中显示每个 State
(例如Oklahoma").
I need to display every State
(e.g. "Oklahoma") in an ArrayList<State>
in a JList
.
我不知道如何做到这一点……尤其是在 NetBeans 中.
I can't figure out how to do this... especially not in NetBeans.
我认为这涉及创建一个 DefaultListModel
,其中每个 State
作为 String
.我已经试过一百万种不同的方法都无济于事.
I think it involves creating a DefaultListModel
with each State
as a String
. I've tried this a million different ways to no avail.
是否可以只加载 ArrayList
(也许如果 State
类有一个 toString()
方法)?这将特别有用,因为我可以通过 actionPerformed()
直接修改 ArrayList
.
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()
.
为了这个问题,让我们假设 State
是一个 name
类型为 String
和一个 population 的对象
类型为 int
.
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
.
我希望这是有道理的.
谢谢.
推荐答案
不幸的是,使用 List 的 JList 唯一简单"的初始化是通过 Vector
Unfortunately, the only 'easy' initialization of JList with a List is via Vector
Vector<State> myStates = new Vector();
//add states
JList list = new JList(myStates);
这篇关于自定义类和 JLists的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!