在 GUI 中列出 ArrayList 中的对象并在多个 ArrayList 之间交替 [英] Listing objects in ArrayList in GUI and alternating between many ArrayLists
问题描述
我目前正在做一个个人项目,在这个项目中,要求用户在3个不同类别下创建对象,所以创建对象后,有一个方法检查对象的类别然后添加它到一个ArrayList.更多详细信息:该对象称为Vehicle,分为三类:两个轮子、三个轮子、三个轮子.所以,当你创建一个新的 Vehicle 时,你会被要求指定你正在创建的车辆类型,比如你说两个轮子,将调用一个方法将对象添加到 ArrayList twoWheelList 中,依此类推.
I'm currently working on a personal project, in this project, the user is asked to create objects under 3 different categories, so after the object is created, there's a method that check's the category of the object and then it adds it to an ArrayList. More Details: the object is called Vehicle, there're three categories: Two wheels, three weels, three wheels. So, when you create a new Vehicle, you'd be asked to specify what kind of vehicles you're creating, say you said two wheels, a method will be called which will add the object into ArrayList twoWheelList, and so on.
我目前有两个问题,我正在为这个程序制作一个GUI,我想在其中显示存储在ArrayLists中的信息,基本上用户有一个comboBox,它有下面三个类别的名称它.我希望当用户选择让我们说两个轮子"时,jList 将列出 ArrayList twoWheelList 中的每个对象
I have two problems at the moment, I'm making a GUI for this program, where I'd like to display information stored in the ArrayLists, basically the user has a comboBox, which has the name of the three categories underneath it. I'd like that when the user chooses let's say "Two Wheels", a jList will list each object in the ArrayList twoWheelList
我怎样才能做到这一点?我已经尝试了几次,但我没有走运.我正在使用 NetBeans,我不太了解整个 JFrame 概念以及如何编写源代码,对于第一个问题,我需要程序知道用户选择了什么,我应该只使用 IF 语句?如何让对象的 ArrayList 显示在 jList 中?
How can I make that happen? I've tried several times but I'm coming out with no luck. I'm using NetBeans, I don't really understand the whole JFrame concept and how to write the source codes, for the first problem where I'd need the program to know what did the user choose, should I just use an IF statement? How can I make the ArrayList of objects to be displayed in the jList?
推荐答案
首先将 ItemListener
添加到 JComboBox
.在 itemStateChanged
事件中,您需要监视 ItemEvent
或 SELECTED
状态.
Start by adding an ItemListener
to the JComboBox
. In the itemStateChanged
event, you will want to monitor the ItemEvent
or the SELECTED
state.
从 JComboBox
中获取选定的值.这可能需要您提供某种外观,可以将组合框中的项目与 ArrayList
相关联,或者,如我所愿,包含项目名称和 的包装类>ArrayList
包装成单个对象,这将提供获取ArrayList
Get the selected value from the JComboBox
. This may require you to provide some kind of look that can associate the item in the combo box with the ArrayList
or, as I would prefer, a wrapper class which contained the name of the item an the ArrayList
wrapped into a single object, which would provide the means to obtain the ArrayList
然后使用自定义ListModel
,您可以将ArrayList
包裹在其中并将其应用到您在屏幕上的JList
..
Then using a custom ListModel
, you can wrap the ArrayList
within it and apply it to the JList
you have on the screen...
public class ProxyListModel extends AbstractListModel<Vehicle> {
private List<Vehicle> vehicles;
public ProxyListModel(List<Vehicle> vehicles) {
// It might be better to simply make a copy of the list
// but that's up to you...
this.vehicles = vehicles;
}
@Override
public int getSize() {
return vehicles.size();
}
@Override
public Vehicle getElementAt(int index) {
return vehicles.get(index);
}
}
查看如何使用组合框和
Check out How to Use Combo Boxes and How to Use Lists for more details. Play close attention to the discussions about custom rendering.
这篇关于在 GUI 中列出 ArrayList 中的对象并在多个 ArrayList 之间交替的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!