如何使用JGoodies
将包含字符串项的JComboBox绑定到bean中的整数值我想以 JComboBox 名称显示并将其绑定到该名称的某个 ID 值。
使用 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
}