在GUI中列出ArrayList中的对象并在许多ArrayList之间交替 [英] Listing objects in ArrayList in GUI and alternating between many ArrayLists
问题描述
我当前正在处理一个个人项目,在这个项目中,要求用户创建3个不同类别的对象,因此在创建对象之后,有一种方法可以检查对象的类别,然后添加它到一个ArrayList. 更多详细信息:该对象称为车辆",分为三类:两个轮子,三个轮胎,三个轮子.因此,当您创建新的Vehicle时,将要求您指定要创建的车辆类型,比如说两个轮子,将调用一个方法,该方法会将对象添加到ArrayList twoWheelList中,依此类推. /p>
此刻我有两个问题,我正在为此程序制作一个GUI,我想在其中显示存储在ArrayLists中的信息,基本上,用户有一个comboBox,其下面有三个类别的名称.它.我希望当用户选择"Two Wheels"时,一个jList将列出ArrayList twoWheelList
中的每个对象我该如何实现?我已经尝试了好几次,但运气不好.我正在使用NetBeans,我不太了解整个JFrame概念以及如何编写源代码,对于第一个问题,我需要程序知道用户选择了什么,我应该只使用IF语句吗? ? 如何使对象的ArrayList显示在jList中?
首先将ItemListener
添加到JComboBox
.在itemStateChanged
事件中,您将要监视ItemEvent
或SELECTED
状态.
从JComboBox
获取选择的值.这可能需要您提供某种外观,以便将组合框中的项目与ArrayList
关联,或者,如我所愿,将包含该项目名称的包装器类和ArrayList
打包为单个对象,它将提供获取ArrayList
然后使用自定义的ListModel
,您可以将ArrayList
包装在其中,并将其应用于屏幕上的JList
...
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);
}
}
查看如何使用组合框和如何使用列表.密切注意有关自定义渲染的讨论.
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.
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
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?
Start by adding an ItemListener
to the JComboBox
. In the itemStateChanged
event, you will want to monitor the ItemEvent
or the SELECTED
state.
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
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屋!