我遇到的问题是我使用带有CellRenderer的JComboBox来显示我的类AddressNode的值。但是,当我从DropDown中选择一个Item时,它会将Object Value插入EditorComponent而不是Rendered Value。我的CellRenderer的代码如下:
public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) {
super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);
if (value instanceof AddressNode) {
AddressNode node = (AddressNode) value;
String displayString = "";
displayString += node.getStreet() + " " + node.getHouse();
if (node.getCity() != null)
displayString += (", " + node.getCity());
if (node.getPostCode() != null)
displayString += (" " + node.getPostCode());
setText(displayString);
}
return this;
}
例如,当我从DropDown列表中选择一个Item时,它会将EditorComponent的值设置为:Model.AddressNode@581280a8。 而我希望它将值设置为我的CellRenderer显示的相同String。
答案 0 :(得分:0)
您可以尝试以下代码吗? public Component getListCellRendererComponent(JList list,Object value,int index,boolean isSelected,boolean cellHasFocus){
if (value instanceof AddressNode) {
AddressNode node = (AddressNode) value;
String displayString = "";
displayString += node.getStreet() + " " + node.getHouse();
if (node.getCity() != null)
displayString += (", " + node.getCity());
if (node.getPostCode() != null)
displayString += (" " + node.getPostCode());
//setText(displayString);
}
返回super.getListCellRendererComponent(list,displayString,index,isSelected,cellHasFocus); }