Java 对象的JComboBox实例中的项
您好,我有以下代码来查看JComboBox中的项是否是类(Persoon)的实例 item的输出是persoon.name变量的值。所以JComboBox中的项目实际上是字符串 这就是JComboBox列表的设置方式Java 对象的JComboBox实例中的项,java,swing,arraylist,hashmap,jcombobox,Java,Swing,Arraylist,Hashmap,Jcombobox,您好,我有以下代码来查看JComboBox中的项是否是类(Persoon)的实例 item的输出是persoon.name变量的值。所以JComboBox中的项目实际上是字符串 这就是JComboBox列表的设置方式 PersoneList.addItem(persoon.getNaam()) 我的问题是。。如何检查此Person对象是否与JComboBox中的对象相同?您应该向JComboBox添加Person对象,而不仅仅是名称,因此在调用object item=event.getItem(
PersoneList.addItem(persoon.getNaam())代码>
我的问题是。。如何检查此Person对象是否与JComboBox中的对象相同?您应该向JComboBox
添加Person
对象,而不仅仅是名称,因此在调用object item=event.getItem()时
这将返回人员
,而不是字符串
。如果要在JComboBox
中显示此人的姓名,请将person
类中的toString
方法重写为如下内容:
public String toString()
return this.naam;
}
您应该将这些项目添加到列表中
personenList.addItem(persoon);
编辑
如果不想(或可以)重写toString
方法,则应使用自定义渲染器。这是一个链接和示例:
您应该在JComboBox
中添加Person
对象,而不仅仅是名称,因此在调用Object item=event.getItem()时
这将返回人员
,而不是字符串
。如果要在JComboBox
中显示此人的姓名,请将person
类中的toString
方法重写为如下内容:
public String toString()
return this.naam;
}
您应该将这些项目添加到列表中
personenList.addItem(persoon);
编辑
如果不想(或可以)重写toString
方法,则应使用自定义渲染器。这是一个链接和示例:
仅用于显示目的的OverridetoString
方法不是一种好的做法。
这也是一个潜在的瓶颈。例如,假设您需要显示两个不同的JComboBox
人员:其中一个只需要显示姓名,另一个需要显示全名。您只能重写Person#toString()
方法一次
通过的方法是使用。例如:
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();
}
});
}
}
如果运行此示例,您将看到如下内容:
public String toString()
return this.naam;
}
正如您所看到的,两个JComboBox
都包含Person
对象,但它们在每一个对象中的表示都不同。仅用于显示目的的OverridetoString
方法不是一个好的做法。
这也是一个潜在的瓶颈。例如,假设您需要显示两个不同的JComboBox
人员:其中一个只需要显示姓名,另一个需要显示全名。您只能重写Person#toString()
方法一次
通过的方法是使用。例如:
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();
}
});
}
}
如果运行此示例,您将看到如下内容:
public String toString()
return this.naam;
}
正如您所看到的,两个JComboBox
都包含Person
对象,但它们在每一个对象中的表示方式都不同。目标是什么,因为有两种三种不同且最简单的方法(不是询问、描述,不是在这里发布的代码中只是猜测),没有SSCCE没有人知道(更好且会说上千个工作)或者详细描述,默认情况下,Swing中的DefaultWhather只返回字符串或对象(所有可能的数据类型都是用Java实现的)其目标是什么,因为(没有询问,没有描述,没有在这里发布的代码中只是猜测)有两种三种不同且最简单的方法,没有SSCCE没有人知道(更好,可以说成千上万个工作)或者关于的详细描述,默认情况下,Swing中的DefaultWhather只返回toString或Object(所有可能的数据类型都是用Java实现的)Mate you rock!这正是我想要的:Dsigh。。。重写toString不是正确的方法。相反,实现一个自定义渲染器,该渲染器使用该属性设置其text@kleopatra你是对的,我只想使用默认的渲染器(调用and对象的toString
)使事情变得简单。尽管如此,我还是更新了我的答案。伙计,你真厉害!这正是我想要的:Dsigh。。。重写toString不是正确的方法。相反,实现一个自定义渲染器,该渲染器使用该属性设置其text@kleopatra你是对的,我只想使用默认的渲染器(调用and对象的toString
)使事情变得简单。尽管如此,我还是更新了我的答案。