I'm trying to populate my JComboBox with the following items:
我正在尝试使用以下项填充我的JComboBox:
public class DropDownItem {
private String text;
private int id;
public void setText(String text) {
this.text = text;
}
public void setId(int id) {
this.id = id;
}
public String toString() {
return text;
}
public int getId() {
return id;
}
public boolean equals(Object i) {
System.out.println("i is: " + i);
if(i instanceof Integer) {
if((Integer)i == (Integer)id) {
System.out.println("It's me!");
return true;
}
else {
System.out.println("I was asked if I was " + (Integer)i + " but I'm " + id + " as I'm " + text);
return super.equals(i);
}
}
else return super.equals(i);
}
}
}
However I'm having trouble using JComboBox's setSelectedItem
. I pass setSelectItem
an int, and as you can see from above, I've tried to make sure that it gets selected when it's the correct one. The problem I'm having, is that only the currently selected item is checked, which to me is very strange. I verified that by added the print statement, which only gets printed out once..
但是我在使用JComboBox的setSelectedItem时遇到了麻烦。我将setSelectItem传递给一个int,正如你从上面所看到的,我试图确保它在正确的时候被选中。我遇到的问题是,只检查当前选中的项目,这对我来说非常奇怪。我通过添加print语句来验证,该语句只打印一次..
Any ideas?
有任何想法吗?
Thanks
谢谢
1 个解决方案
#1
3
Your implementation of the equals() method is wrong. The Object will never be an Integer, it will always be a DropDownItem. I would guess equality would be checked by comparing the "id" of the current Object with the "id" of the Object passed to the equals() method.
你的equals()方法的实现是错误的。 Object永远不会是一个Integer,它永远是一个DropDownItem。我想通过比较当前Object的“id”和传递给equals()方法的Object的“id”来检查相等性。
Edit: If you add a new item to the model and want to select it your code should be something like:
编辑:如果您向模型添加新项目并想要选择它,您的代码应该是这样的:
DropDownItem item = new DropDownItem();
item.setId(1);
item.setText("one");
comboBox.addItem( item );
comboBox.setSelectedItem( item );
Edit2: the equals method will look something like:
Edit2:equals方法看起来像:
DropDownItem item = (DropDownItem)i;
return getId() == item.getId();
Now when you get the integer value from the database you can just do:
现在,当您从数据库中获取整数值时,您可以执行以下操作:
DropDownItem item = new DropDownItem();
item.setId(???);
comboBox.setSelectedItem( item );
Even though you didn't specify the description, the item will be selected because the equals method only cares about the id.
即使您没有指定描述,也会选择该项,因为equals方法只关注id。
#1
3
Your implementation of the equals() method is wrong. The Object will never be an Integer, it will always be a DropDownItem. I would guess equality would be checked by comparing the "id" of the current Object with the "id" of the Object passed to the equals() method.
你的equals()方法的实现是错误的。 Object永远不会是一个Integer,它永远是一个DropDownItem。我想通过比较当前Object的“id”和传递给equals()方法的Object的“id”来检查相等性。
Edit: If you add a new item to the model and want to select it your code should be something like:
编辑:如果您向模型添加新项目并想要选择它,您的代码应该是这样的:
DropDownItem item = new DropDownItem();
item.setId(1);
item.setText("one");
comboBox.addItem( item );
comboBox.setSelectedItem( item );
Edit2: the equals method will look something like:
Edit2:equals方法看起来像:
DropDownItem item = (DropDownItem)i;
return getId() == item.getId();
Now when you get the integer value from the database you can just do:
现在,当您从数据库中获取整数值时,您可以执行以下操作:
DropDownItem item = new DropDownItem();
item.setId(???);
comboBox.setSelectedItem( item );
Even though you didn't specify the description, the item will be selected because the equals method only cares about the id.
即使您没有指定描述,也会选择该项,因为equals方法只关注id。