我有以下问题:
我想在我的视图中将一个 JCombobox 与我的模型“DomainModel”绑定,以便以后可以使用“AnotherModel”获取它。getModel();
我写了一个自己的 CellRenderer 让它看起来像“ID - Name”。但是当我选择 Combobox 的值并调用 'AnotherModel'.getModel 时,该值不会保存在其中。
不能用 JGoodies Bindings 绑定复杂的数据类型吗?使用 String 可以正常工作,但我想绑定一个“DomainModel”对象
这是简化的代码:
风景:
public class View extends JPanel {
private JComboBox<DomainModel> cmbValueModel;
public View(AntotherModel antotherModel, List<DomainModel> manyDomainModels) {
PresentationModel<DomainModel> domainModel = new PresentationModel<DomainModel>();
domainModel.setBean(antotherModel.getModel());
cmbValueModel = BasicComponentFactory.createComboBox(new SelectionInList<DomainModel>(manyDomainModels, domainModel.getModel(DomainModel.PROPERTYNAME_NAME)));
Bindings.bind(cmbValueModel, new SelectionInList<>(), "");
cmbValueModel.setRenderer(new DefaultListCellRenderer(){
@Override
public Component getListCellRendererComponent(JList<?> list,
Object value, int index, boolean isSelected,
boolean cellHasFocus) {
return super.getListCellRendererComponent(list, value == null ? null : ((DomainModel)value).getId() + " - " + ((DomainModel)value).getName() , index, isSelected,
cellHasFocus);
}
});
}
}
域名:
public class DomainModel extends Model{
public static final String PROPERTYNAME_NAME = "name";
@GeneratedValue
private int id;
private String name;
public void setName(String name) {
String oldVal = this.name;
this.name = name;
changeSupport.firePropertyChange(PROPERTYNAME_NAME, oldVal, name);
}
public String getName(){
return name;
}
public int getId(){
return id;
}
}
另一种型号:
public class AntotherModel extends Model{
public static final String PROPERTYNAME_MODEL = "model";
private int id;
private DomainModel model;
public int getId() {
return id;
}
public DomainModel getModel() {
return model;
}
public void setId(int id) {
this.id = id;
}
public void setModel(DomainModel model) {
DomainModel oldVal = this.model;
this.model = model;
changeSupport.firePropertyChange(PROPERTYNAME_MODEL, oldVal, model);
}
}