JComboBox对象实例中的项

时间:2013-09-30 12:55:22

标签: java swing arraylist hashmap jcombobox

您好我有以下代码来查看JComboBox中的项是否是类(Persoon)的实例。

    public class ItemChangeListener implements ItemListener {

        Persoon selectedPerson;
        RekeningApp app;
        PersoonView view;

        public ItemChangeListener(PersoonView view) {

            this.view = view;

        }

        public void itemStateChanged(ItemEvent event) {
            if (event.getStateChange() == ItemEvent.SELECTED) {
                Object item = event.getItem();
                System.out.println("Itemchangelistener " + item);
                // do something with object
                if(item instanceof Persoon) {
                    System.out.println("Instance");
                    this.selectedPerson = (Persoon) item;
                    view.setOverzicht(this.selectedPerson);
                } else {
                    this.selectedPerson = null;
                }
            }
        }

    }

item的输出是persoon.name变量的值。所以JComboBox中的项目实际上是字符串。

这是JComboBox列表的设置方式。

personenList.addItem(persoon.getNaam());

我的问题是..如何检查这个Persoon对象是否存在且与JComboBox中的相同?

2 个答案:

答案 0 :(得分:9)

您应该添加JComboBox PersonObject item = event.getItem();个对象,而不仅仅是名称,因此当您致电Person时,这将返回String,而不是{{1} }}。如果您想在JComboBox中显示此人的姓名,请将toString课程中的Person方法覆盖为以下内容:

public String toString()
    return this.naam;
}

您应该将这些项目添加到列表中。

personenList.addItem(persoon);   

修改

如果您不想(或可以)覆盖toString方法,则应使用自定义渲染器。这是一个链接和示例:

http://docs.oracle.com/javase/tutorial/uiswing/components/combobox.html#renderer

答案 1 :(得分:6)

仅仅为了显示目的而覆盖toString方法不是一个好习惯。 这也是一个潜在的瓶颈。让我们举例来说,你需要向人们展示两个不同的JComboBox:在其中一个中你需要只显示名字,而另一个你需要显示全名。您只能覆盖Person#toString()方法一次。

经历的方法是使用ListCellRenderer。例如:

public class Person {
    private String _name;
    private String _surname;

    public Person(String name, String surname){
        _name = name;
        _surname = surname;
    }

    public String getName() {
        return _name;
    }

    public String getSurname() {
        return _surname;
    }    
}

这是GUI:

import java.awt.Component;
import java.awt.GridLayout;
import javax.swing.DefaultComboBoxModel;
import javax.swing.DefaultListCellRenderer;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;

public class Demo {

    private void initGUI(){
        Person person1 = new Person("First", "Person");
        Person person2 = new Person("Second", "Person");
        Person[] persons = new Person[]{person1, person2};

        /* 
         * This combo box will show only the person's name
         */
        JComboBox comboBox1 = new JComboBox(new DefaultComboBoxModel(persons));
        comboBox1.setRenderer(new DefaultListCellRenderer() {
            @Override
            public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) {
                super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);
                if(value instanceof Person){
                    Person person = (Person) value;
                    setText(person.getName());
                }
                return this;
            }
        } );

        /* 
         * This combo box will show person's full name
         */
        JComboBox comboBox2 = new JComboBox(new DefaultComboBoxModel(persons));
        comboBox2.setRenderer(new DefaultListCellRenderer() {
            @Override
            public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) {
                super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);
                if(value instanceof Person){
                    Person person = (Person) value;
                    StringBuilder sb = new StringBuilder();
                    sb.append(person.getSurname()).append(", ").append(person.getName());
                    setText(sb.toString());
                }
                return this;
            }
        } );

        JPanel content = new JPanel(new GridLayout(2, 2));
        content.add(new JLabel("Name:"));
        content.add(comboBox1);
        content.add(new JLabel("Surname, Name:"));
        content.add(comboBox2);

        JFrame frame = new JFrame("Demo");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setContentPane(content);
        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);

    }

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

            @Override
            public void run() {
                new Demo().initGUI();
            }
        });

    }
}

如果您运行此示例,您将看到如下内容:

enter image description here

正如您所看到的,JComboBox包含Person个对象,但每个对象的表示形式各不相同。

相关问题