[英]How to create a JList that contains an object which has more than one value? e.g. Price, Weight, Colour etc
i'm pretty much a newbie to programming so this may seem like a very stupid question, I've been trying to do this for a while but cant get my head around it. 我几乎是编程的新手,所以这似乎是一个非常愚蠢的问题,我已经尝试了一段时间,但是却无法解决。
But I was wondering if its possible for the JList object to store more than one value, so when i click on the object in the list, the different values such as price, weight, colour etc. will be then displayed into some JTextField's below the list. 但是我想知道JList对象是否可以存储多个值,因此当我单击列表中的对象时,不同的值(例如价格,重量,颜色等)将显示在JTextField的下面。清单。
And would it then be possible to then add that JList object into another JList? 然后可以将那个JList对象添加到另一个JList中吗?
I've been researching on how to do this but I cant seem to find it anywhere. 我一直在研究如何做到这一点,但我似乎找不到任何地方。 Is there an easier approach to doing this?
有没有更简单的方法可以做到这一点?
Thanks in advance!:) 提前致谢!:)
So this is basically what i want it to look like: 所以这基本上是我想要的样子:
But I was wondering if its possible for the JList object to store more than one value, so when i click on the object in the list, the different values such as price, weight, colour etc. ..
但是我想知道JList对象是否可以存储多个值,所以当我单击列表中的对象时,将显示不同的值,例如价格,重量,颜色等。
Yes & no - but more yes than no. 是&否-但是,是比没有更多。 To explain:
解释:
What is required here is to define a new class that encapsulates that information. 这里需要的是定义一个封装该信息的新类。 It might be something like a 'menu item'.
它可能类似于“菜单项”。 That (let's roll with that)
MenuItem
class then has fields to store the description, price, calories and other attributes of that menu item. 这(让我们与卷)
MenuItem
,然后类有字段存储描述,价格,卡路里和菜单项的其他属性。
Then the list simply has to be configured to handle a MenuItem
. 然后,只需将列表配置为处理
MenuItem
。 Configuring it might not be necessary if the toString()
method of the class shows all the information you would want to show in the list. 如果该类的
toString()
方法显示了您希望在列表中显示的所有信息,则可能不需要进行配置。 Otherwise look to use a custom renderer to show exactly what is required. 否则,请使用自定义渲染器准确显示所需的内容。
The rest, displaying the attributes in fields and moving from list ( MenuList
) to list ( MealOrder
) is pretty straightforward, and covered in the relevant tutorials. 其余的在字段中显示属性并从列表(
MenuList
)移到列表( MealOrder
)非常简单,并在相关教程中进行了介绍。
JList can have any Object as its item members; JList可以将任何Object作为其项目成员; what shows in the list is the string from that object's toString() method.
列表中显示的是该对象的toString()方法中的字符串。 So you make any subclass of Object that can have any number of fields, and override toString() to return whatever string you want to see in the list..
因此,您可以使Object的任何子类具有任意数量的字段,并重写toString()以返回要在列表中看到的任何字符串。
It is possible of course, what you need is a class to stock the object's states/fields. 当然,您可能需要一个类来存储对象的状态/字段。
Here is a class that show approximatively what you want. 这是一个大致显示您想要的课程。
When the item is selected, a dialog shows up showing the characteristics of it. 选择该项目后,将显示一个对话框,显示该项目的特征。
The same behavior is reproducible within the program you'll be making. 在您将要制作的程序中,可以重现相同的行为。
import java.awt.Color;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JFrame;
import javax.swing.JList;
import javax.swing.JOptionPane;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
public class Test {
public static class Item {
String name;
double price;
double weight;
Color color;
public Item(String n,double p, double w, Color c){
name = n;
price = p;
weight = w;
color = c;
}
public String extendedToString() {
return name + " " + price + " " + weight + " " + color;
}
@Override
public String toString() {
return this.name;
}
}
public static void main(String[] args) {
JFrame f = new JFrame();
Item i = new Test.Item("myItem", 5d, 2d, Color.black);
JList<Item> l = new JList<Item>(new Item[]{i});
l.addListSelectionListener(new ListSelectionListener(){
@Override
public void valueChanged(ListSelectionEvent e) {
JList l = (JList) e.getSource();
Item item = (Item) l.getSelectedValue();
JOptionPane.showMessageDialog(f, item.extendedToString());
}
});
f.add(l);
f.setSize(500, 500);
f.setVisible(true);
}
}
It is possible. 有可能的。
When your model grows and you start wondering how you should handle all of these listeners and propagate the changes, start looking for an in process EventBus . 当模型增长时,您开始想知道如何处理所有这些侦听器并传播更改,开始寻找进程中的EventBus 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.