简体   繁体   English

如何创建一个包含多个对象的JList? 例如价格,重量,颜色等

[英]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: 所以这基本上是我想要的样子:

http://i.stack.imgur.com/lXclt.png

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);
    }
}

Before 之前

在此处输入图片说明


After

在此处输入图片说明

It is possible. 有可能的。

  1. Build a Model Class that holds the attributes you mentioned above for each item. 构建一个模型类,其中包含您上面为每个项目提到的属性。
  2. Realize a ListCellRenderer for showing the item name (and price) of each item and use an instance of it for Starters-, Mains- and Desserts-Lists. 实现一个ListCellRenderer来显示每个项目的项目名称(和价格),并将其实例用于Starters-,Mains-和Desserts-List。 The DefaultListCellRenderer is a good way to start. DefaultListCellRenderer是一个很好的开始。 Put the Items in a ListModel and set it to your JList . 将项目放入ListModel并将其设置为JList
  3. Do the same for the Bill List on the right. 对右侧的清单清单执行相同的操作。
  4. Add a ListSelectionListener that informs you about the last selected item. 添加一个ListSelectionListener ,它通知您有关最后选择的项目。

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.

相关问题 使用该类是否更有效,例如Hashtable而不是接口,例如Map? - Is it more efficient to use the class, e.g. Hashtable than the interface, e.g. Map? 如何在jList中存储多个字符串 - How to store more than one string in jList 如何以编程方式动态创建所有Android ui组件的列表?例如TextView,ImageView等 - How can I dynamically create a list of all Android ui components programmatically? e.g. TextView, ImageView etc 如何在Android中使用InputStream对象创建受Android支持的视频文件(例如mp4) - How to Create android supported video file (e.g. mp4) using InputStream object in Android 在FreeMarker中,如何自动将自定义对象(例如java.awt.Color)转换为特定的String值(例如HTML十六进制颜色)? - In FreeMarker, how do I automatically convert a custom object (e.g. java.awt.Color) to a particular String value (e.g. HTML hex color)? 为什么Java equals(Object O)方法没有可将特定对象类型(例如String,Integer等)作为输入的变量? - Why does the Java equals(Object O) method not have a variant which can take a specific object type (e.g. String, Integer, etc) as input? 如何为BitSet类型的元素创建SortedSet(例如TreeSet) - How to create SortedSet (e.g. TreeSet) for elements of type BitSet 如何为Spring REST控制器设置多个基本路径(例如,一个或多个软件包的通用基本路径) - How to set multiple base paths for Spring REST controllers (e.g. common base path for one or more packages) 如何创建包含字符串和对象的Hashtable条目的JList? - How can I create a JList that contains entries of a Hashtable of String and Object? 如何检查Set是否包含一个成员变量等于某个值的对象 - How to check if a Set contains an object which has one member variable equal to some value
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM