1

你能告訴我爲什麼我在這裏得到ClassCastException異常嗎?ClassCastException Beansbinding Java

protected void initDataBindings() { 
    BeanProperty<Apprentice, String> apprenticeBeanProperty = BeanProperty.create("vorname"); 
    ObjectProperty<JTextField> jTextFieldObjectProperty = ObjectProperty.create(); 
    AutoBinding<Apprentice, String, JTextField, JTextField> autoBinding = Bindings.createAutoBinding(UpdateStrategy.READ_WRITE, apprentice, apprenticeBeanProperty, txtVorname, jTextFieldObjectProperty); 
    autoBinding.bind(); 
} 

這是文本字段:

 { 
      txtVorname = new JTextField(); 
      //txtVorname.setPreferredSize(new Dimension(txtVorname.getPreferredSize().width + 160, txtVorname.getPreferredSize().height)); 
      GridBagConstraints gbc = new GridBagConstraints(); 
      gbc.gridx = 1; 
      gbc.gridy = 0; 
      gbc.fill = GridBagConstraints.HORIZONTAL; 
      rightPanel.add(txtVorname, gbc); 
     } 

Apprentice.java具有attributee 「vorname」(德語爲姓名),其是與getter和setter私有字段。

private String vorname; 

public Apprentice(){ 
    ; 
} 

public String getVorname() { 
    return vorname; 
} 

public void setVorname(String vorname) { 
    this.vorname = vorname; 
} 

回答

0

我自己找到了解決辦法,我綁定了「自我對象」,而不是「文本」是有這個樣子:

protected void initDataBindings() { 
    BeanProperty<Apprentice, String> apprenticeBeanProperty_1 = BeanProperty.create("vorname"); 
    BeanProperty<JTextField, String> jTextFieldBeanProperty_1 = BeanProperty.create("text"); 
    AutoBinding<Apprentice, String, JTextField, String> autoBinding_1 = Bindings.createAutoBinding(UpdateStrategy.READ_WRITE, apprentice, apprenticeBeanProperty_1, txtVorname, jTextFieldBeanProperty_1); 
    autoBinding_1.bind(); 
} 

不管怎樣,謝謝:)