java 如何将对象添加到 JList 并在列表界面上向用户显示该对象的成员?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12478661/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 09:04:12  来源:igfitidea点击:

How to dd an object to the JList and show member of the object on the list interface to the user?

javaswing

提问by Bernard

I have an ArrayList of object which is type of the cd. I want to add all the objects to the JList and show the field of name to the user. I can add the String type to the JList but what about specific field of the object?

我有一个 ArrayList 对象,它是 cd 类型。我想将所有对象添加到 JList 并向用户显示名称字段。我可以将 String 类型添加到 JList 但对象的特定字段呢?

CD is:

光盘是:

class CD{
    public CD(String n){name = n;}
    private String name;
    public String getName(){return name;}
    public void setName(String n){name = n;}
}

ArrayList is:

数组列表是:

ArrayList<CD> myList = new ArrayList<CD>();

And now I want to add myList to a JList:

现在我想将 myList 添加到 JList:

JList list = new JList(myList);
panel.add(list);
JScrollPane scrol = new JScrollPane(list);
frame.add(scrol,BorderLayout.EAST);
frame.add(panel);
frame.setVisible(true);

First of all, is this way correct? Secondly how can I show the user name field of the object in the list? my desired interface is :

首先,这种方式正确吗?其次,如何在列表中显示对象的用户名字段?我想要的界面是:

enter image description here

在此处输入图片说明

The left side is the name of my object! Thanks in advance. Bernard

左边是我对象的名字!提前致谢。伯纳德

回答by Guillaume Polet

Use a custom renderer based on the DefaultListCellRenderer:

使用基于 DefaultListCellRenderer 的自定义渲染器:

import java.awt.Component;
import java.util.ArrayList;
import java.util.List;
import java.util.Vector;

import javax.swing.DefaultListCellRenderer;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JScrollPane;
import javax.swing.SwingUtilities;

public class TestJList {

    class CD {
        public CD(String n) {
            name = n;
        }

        private String name;

        public String getName() {
            return name;
        }

        public void setName(String n) {
            name = n;
        }
    }

    protected void initUI() {
        List<CD> cds = new ArrayList<CD>();
        cds.add(new CD("MJ - Bad"));
        cds.add(new CD("Mozart - Concerto 123"));
        cds.add(new CD("Nadeah - Odile"));
        JFrame frame = new JFrame(TestJList.class.getSimpleName());
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        JList list = new JList(new Vector<CD>(cds));
        list.setVisibleRowCount(10);
        list.setCellRenderer(new DefaultListCellRenderer() {
            @Override
            public Component getListCellRendererComponent(JList<?> list, Object value, int index, boolean isSelected, boolean cellHasFocus) {
                Component renderer = super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);
                if (renderer instanceof JLabel && value instanceof CD) {
                    // Here value will be of the Type 'CD'
                    ((JLabel) renderer).setText(((CD) value).getName());
                }
                return renderer;
            }
        });
        frame.add(new JScrollPane(list));
        frame.pack();
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {

            @Override
            public void run() {
                new TestJList().initUI();
            }
        });
    }
}

回答by m4tx

I think that if you want to work with the CDclass directly with JList, you'll need to write your own renderer, that will work with CDclass. The renderer may extend the JLabeland display the contents of the namefield.

我认为,如果您想CD直接使用 类使用JList,则需要编写自己的渲染器,它可以与CD类一起使用。渲染器可以扩展JLabel和显示name字段的内容。

There's also other way: for()that iterates over the ArrayListand adds only the namefield to the DefaultListRenderer. The way that you'll do it is your choice.

还有其他方法:for()迭代ArrayList并且只将name字段添加到DefaultListRenderer. 您将采取何种方式进行操作是您的选择。

You can also add ListSelectionListenerto show the album details of the selected list item.

您还可以添加ListSelectionListener以显示所选列表项的专辑详细信息。

All that you need to know is here:

您需要知道的一切都在这里:

// Thanks for kleopatra :)

// 感谢 kleopatra :)

回答by James

There is no constructor for JList that takes an ArrayList, however you can use a Vector or an Array (and ArrayList can easily be converted to an Array).

JList 没有接受 ArrayList 的构造函数,但是您可以使用 Vector 或 Array(并且 ArrayList 可以轻松转换为 Array)。

to display what you want to the user, You could use a custom renderer. Alternatively you could just override the toString() method of CD which, if I recall correctly, is the default method to use to determine what to show.

要向用户显示您想要的内容,您可以使用自定义渲染器。或者,您可以覆盖 CD 的 toString() 方法,如果我没记错的话,它是用于确定要显示的内容的默认方法。