简体   繁体   English

Java / Swing-将组件添加到JList

[英]Java/Swing - Add a Component to a JList

I have a JList that I need to add Components to. 我有一个JList,我需要向其中添加组件。 What I have done is I have made a DefaultListModel that takes the type of a Component I have made. 我所做的是我制作了一个DefaultListModel,它采用了我制作的组件的类型。 My code adds it to the DefaultListModel, and it does display information, but it is just the components name in String format. 我的代码将其添加到DefaultListModel,并且确实显示信息,但它只是String格式的组件名称。 How should I make it actually display the component instead of the component's name? 我应该如何使其实际显示组件而不是组件名称? Is it even possible? 可能吗

Here is my code 这是我的代码

    DefaultListModel<CustomComponent> jListModel = new DefaultListModel<>();
jListModel.addElement(new CustomComponent()); //Adds the name of the component(not what I want)

Implement your ListCellRenderer class In the method 在方法中实现ListCellRenderer类

Component getListCellRendererComponent(
    JList<? extends E> list,
    E value,
    int index,
    boolean isSelected,
    boolean cellHasFocus);

All you need is to return value . 您需要做的就是返回value The value is list element in your case it's the CustomComponent instance. 在您的情况下,值为list元素是CustomComponent实例。 It's simplest approach. 这是最简单的方法。

But it's not correct approach. 但这不是正确的方法。 List model should keep data (not component). 列表模型应保留数据(而非组件)。 Instead define one instance of CustomComponent for the renderer and in the getListCellRendererComponent() call something like customComponentInstance.init(value) to let the CustomComponent reflect data from model. 而是为渲染器定义一个CustomComponent实例,然后在getListCellRendererComponent()调用诸如customComponentInstance.init(value)以使CustomComponent反映模型中的数据。

It is possible. 有可能的。 You need to write your own ListCellRenderer, which returns your Component, instead of the default JLabel. 您需要编写自己的ListCellRenderer,它返回您的Component,而不是默认的JLabel。

Example: 例:

public class ComponentListCellRenderer {

    public ComponentListCellRenderer() {
    }

    public Component getListCellRendererComponent(JList<?> list, Object value, int            index, boolean isSelected, boolean cellHasFocus) {
    Component c = super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);
        if (value instanceof Component){
            c = (Component) value;
            c.setPreferredSize(c.getSize());
        }

        return c;
    }

}

In this case the default Component c is intercepted and exchange with your own Component, which would be value . 在这种情况下,默认的Component c被拦截并与您自己的Component交换,该组件将为value We need to set the preferred size of your Component to it´s size, or else the JList won´t display the Component not correctly. 我们需要将组件的preferred size设置为其大小,否则JList将无法正确显示组件。

How to use: 如何使用:

  • Create a new JList<CustomComponent>() 创建一个新的JList<CustomComponent>()
  • set it´s ListCellRenderer to a new ComponentListCellRenderer 将其ListCellRenderer设置为新的ComponentListCellRenderer

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM