Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/315.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/maven/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 对象arraylist中的JComboBox值_Java_Arraylist - Fatal编程技术网

Java 对象arraylist中的JComboBox值

Java 对象arraylist中的JComboBox值,java,arraylist,Java,Arraylist,最近我遇到了一个问题,我让ArrayList充满了对象 ArrayList<Item> allItems; 但这只给了我一些类似的东西。Item@283ae01例如。是否可能只显示组合框中ArrayList中每个项目的值字符串名称?默认渲染器使用列表中对象的toString方法渲染项目。您可以a)重写添加到JComboBox的类的toString方法,以提供名称的正确呈现 @Override public String toString(){ return name; }

最近我遇到了一个问题,我让ArrayList充满了对象

ArrayList<Item> allItems;

但这只给了我一些类似
的东西。Item@283ae01
例如。是否可能只显示组合框中ArrayList中每个项目的值
字符串名称

默认渲染器使用列表中对象的toString方法渲染项目。您可以a)重写添加到JComboBox的类的toString方法,以提供名称的正确呈现

@Override
public String toString(){
    return name;
}
或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);
注:以上是java 7之前的语法。在Java7及更高版本中,这些类使用泛型参数化

您可以进一步创建由列表支持的您自己的模型,因此,当项目添加到列表中时,您可以启动相应的侦听器来通知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);