Java 对象的 JComboBox 实例中的项目
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19094845/
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
Item in JComboBox instance of an object
提问by Reshad
Hello I have the following code to see if an item in the JComboBox is instance of a class(Persoon).
您好,我有以下代码来查看 JComboBox 中的项目是否是类(Person)的实例。
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;
}
}
}
}
The output of item is the value of persoon.name variable. so the items in the JComboBox are actually strings.
item 的输出是 persoon.name 变量的值。所以 JComboBox 中的项目实际上是字符串。
this is how the JComboBox list is set.
这就是 JComboBox 列表的设置方式。
personenList.addItem(persoon.getNaam());
personenList.addItem(persoon.getNaam());
My question is.. how can I check If this Persoon object excists and is the same as in the JComboBox?
我的问题是..我如何检查这个 Persoon 对象是否存在并且与 JComboBox 中的相同?
采纳答案by Omar Mainegra
You should add to the JComboBox
the Person
objects, not just the name, so when you call Object item = event.getItem();
this will return the Person
, not an String
. If you want to display the person's name in the JComboBox
, override the toString
method in Person
class to something like this:
您应该添加到JComboBox
的Person
,当你打电话的对象,不只是名字,所以Object item = event.getItem();
这将返回Person
,而不是一个String
。如果要在 中显示此人的姓名,请将类中JComboBox
的toString
方法覆盖为Person
如下所示:
public String toString()
return this.naam;
}
And you should add the items to the list.
您应该将项目添加到列表中。
personenList.addItem(persoon);
Edit
编辑
If you don't want (or can) override the toString
method you should use a custom renderer. This is a link to and example:
如果您不想(或可以)覆盖该toString
方法,则应使用自定义渲染器。这是一个链接和示例:
http://docs.oracle.com/javase/tutorial/uiswing/components/combobox.html#renderer
http://docs.oracle.com/javase/tutorial/uiswing/components/combobox.html#renderer
回答by dic19
Override toString
method just for display purposes isn't a good practice.
Also it's a potential bottleneck. Lets say for example you need to show two different JComboBox
with persons: in one of them you need to show only the name and in the other one you need to show fullname. You can override Person#toString()
method only one time.
toString
仅用于显示目的的覆盖方法不是一个好习惯。这也是一个潜在的瓶颈。例如,您需要显示两个不同JComboBox
的人:在其中一个中您只需要显示姓名,而在另一个中您需要显示全名。您只能覆盖Person#toString()
方法一次。
The way to go through is using a ListCellRenderer. Example:
方法是使用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;
}
}
And here is the GUI:
这是 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();
}
});
}
}
If you run this example you'll see something like this:
如果您运行此示例,您将看到如下内容:
As you can see both JComboBox
contain Person
objects but their representation is different in each one.
如您所见,两者都JComboBox
包含Person
对象,但它们的表示在每个中都不同。