如何使用JGoodies将包含String项的JComboBox绑定到bean中的Integer值 我想以JComboBox名称显示并将其绑定到该名称的某个ID值。
答案 0 :(得分:0)
使用MVP架构,您将拥有一个Presentation SelectInList,它会保留您要列出的对象列表:例如:
class MyObject {
private Integer id;
private String name;
...
//getters and setters
}
class MyView {
private MyPresentationModel;
private JComboBox myComboBox;
...
private void buildComponents {
myComboBox = BasicComponentFactory.createComboBox(getPresentationModel().getMyObjectsSelectionInList(), new ListCellRenderer() {
protected DefaultListCellRenderer defaultRenderer = new DefaultListCellRenderer();
@Override
public Component getListCellRendererComponent(JList list, Object value,
int index, boolean isSelected, boolean cellHasFocus) {
JLabel renderer = (JLabel) defaultRenderer
.getListCellRendererComponent(list, value, index, isSelected,
cellHasFocus);
renderer.setText(((MyObject) value).getName()); //this is point
return renderer;
}
});
}
}
class MyPresentationModel extends com.jgoodies.binding.PresentationModel {
private SelectionInList myObjetcsSelectionInList;
private List<MyObject> list;
private MyModel myModel;
public MyPresentationModel(MyModel myModel) {
this.myModel = myModel;
list = //LOAD LIST
}
public SelectionInList getPeriodTypeSelectionInList() {
if (myObjetcsSelectionInList == null) {
myObjetcsSelectionInList = new SelectionInList(list.toArray(new MyObejct[list.size()]), getModel(MyModel.PROPERTY_MY_OBJECT));
myObjetcsSelectionInList.setSelectionIndex(0);
}
return myObjetcsSelectionInList;
}
...
}
class MyModel {
static public String PROPERTY_MY_OBJECT = "myObject";
private MyObject myObject;
// getters and setters
}