简体   繁体   English

来自对象数组列表的 JComboBox 值

[英]JComboBox values from object arraylist

lately I have come to a problem where I have ArrayList filled with objects最近我遇到了一个问题,我用对象填充了 ArrayList

ArrayList<Item> allItems;

while each of Item objects has its own attributes like String name or int value .而每个 Item 对象都有自己的属性,如String nameint value In my graphical interface I want to have JComboBox filled only with name value of every Item object in allItems ArrayList.在我的图形界面中,我希望 JComboBox 只填充allItems ArrayList 中每个Item对象的名称值。 Objects inside of allItems are added in runtime of my program and it has no values in the beginning. allItems中的对象是在我的程序运行时添加的,它在开始时没有值。 After I add a new item into my ArrayList I call在我将新项目添加到我的 ArrayList 后,我​​调用

comboBox.setModel(new javax.swing.DefaultComboBoxModel(allItems.toArray()));

but this only gives me something like items.Item@283ae01 for example.但这只会给我一些类似items.Item@283ae01东西。 Is it possible to have displayed only the values String name of each item from ArrayList in ComboBox?是否可以只显示 ComboBox 中 ArrayList 中每个项目的值String name

The default renderer uses the toString method of the objects in the list to render the items.默认渲染器使用列表中对象的 toString 方法来渲染项目。 You can either a) Override to the toString method of the class added to the JComboBox to provide the proper rendering of the name您可以 a) 覆盖添加到 JComboBox 的类的 toString 方法以提供名称的正确呈现

@Override
public String toString(){
    return name;
}

or b) provide a custom renderer for the JComboBox which customizes the Component used to render the items in the JComboBox或 b) 为 JComboBox 提供自定义渲染器,该渲染器自定义用于渲染 JComboBox 中的项目的组件

ListCellRenderer renderer = new DefaultListCellRenderer(){
    @Override
    public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) {
        Component c = super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);
        if ( value == null ){
            return c;
        }
        if ( c instanceof JLabel ){
            JLabel label = (JLabel)c;
            Test t = (Test)value;
            label.setText(t.getName());
        }
        return c;
    }
};
myComboBox.setRenderer(renderer);

Note the above is pre-java 7 syntax.请注意,以上是 java 7 之前的语法。 In java 7 and above these classes are parametized using generics.在 java 7 及更高版本中,这些类使用泛型进行参数化。

You could further create your own model backed by the List, so when items are added to the List you fire the appropriate listeners to notify the JComboBox of the change (rather than recreating the model every time).您可以进一步创建由 List 支持的自己的模型,因此当项目添加到 List 时,您会触发适当的侦听器以通知 JComboBox 更改(而不是每次都重新创建模型)。

There are other examples for this and more in the java tutorial on using JComboBoxes 在使用 JComboBoxesjava 教程中还有其他示例以及更多示例

You need to create a custom renderer to display the appropriate property from your class.您需要创建一个自定义渲染器来显示您的类中的适当属性。 However the custom renderer is only half the solution since you will break the default functionality of the combo box to select an item using the keyboard.然而,自定义渲染器只是解决方案的一半,因为您将破坏组合框的默认功能以使用键盘选择项目。

See Combo Box With Custom Renderer for an implementation that does the rendering and still allows the keyboard selection of and item in the combo box.请参阅带有自定义渲染器的组合框,了解执行渲染并仍允许组合框中的键盘选择和项目的实现。

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

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