2012-12-10 51 views
0

我有以下问题的JComboBox和默认值的对象:JGoodies数据绑定:绑定与

我想在我看来,我的模式“的DomainModel”绑定的JComboBox,这样我可以得到稍后得到它“ AnotherModel'.getModel();

我写了一个自己的CellRenderer,使它看起来像'ID - Name'。但是,当我选择Combobox的值并且我调用'AnotherModel'.getModel时,该值不会保存在其中。

是不是可以将复杂的数据类型与JGoodies绑定绑定?用绳子它工作正常,但我要绑定的对象的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); 
     } 



    } 

回答

0

你的代码不能编译或运行,所以很难弄清楚你想要做什么。下面的例子显示了一个组合框绑定到DomainModels列表,如果这是你在之后;您可以将一个valueChangeListener添加到selectionInList以在用户更改选择时执行某些操作。 HTH

import java.awt.Component; 
import java.util.ArrayList; 
import java.util.List; 

import javax.swing.DefaultListCellRenderer; 
import javax.swing.JComboBox; 
import javax.swing.JFrame; 
import javax.swing.JList; 
import javax.swing.JPanel; 

import com.jgoodies.binding.adapter.BasicComponentFactory; 
import com.jgoodies.binding.list.SelectionInList; 

public class View extends JPanel { 

    private JComboBox comboBox; 

    public View(AnotherModel anotherModel, List<DomainModel> manyDomainModels) { 
     final SelectionInList<DomainModel> selectionInList = new SelectionInList<DomainModel>(manyDomainModels); 
     comboBox = BasicComponentFactory.createComboBox(selectionInList); 

     comboBox.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); 
      } 
     }); 

     add(comboBox); 
    } 

    public static void main(String[] args) { 
     final JFrame frame = new JFrame(); 
     final AnotherModel anotherModel = new AnotherModel(); 
     final List<DomainModel> manyDomainModels = new ArrayList<DomainModel>(); 
     final DomainModel domainModel1 = new DomainModel(); 
     domainModel1.setName("foo"); 
     final DomainModel domainModel2 = new DomainModel(); 
     domainModel2.setName("bar"); 
     final DomainModel domainModel3 = new DomainModel(); 
     domainModel3.setName("baz"); 
     manyDomainModels.add(domainModel1); 
     manyDomainModels.add(domainModel2); 
     manyDomainModels.add(domainModel3); 
     frame.getContentPane().add(new View(anotherModel, manyDomainModels)); 
     frame.pack(); 
     frame.setVisible(true); 
    } 
}