ClassCastException Beansbinding Java

时间:2012-11-21 19:01:47

标签: java user-interface jtextfield classcastexception beans-binding

你能告诉我为什么我在这里得到ClassCastException例外吗?

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 具有属性“vorname”(德语为firstname),它是具有getter和setter的私有字段。

private String vorname;

public Apprentice(){
    ;
}

public String getVorname() {
    return vorname;
}

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

1 个答案:

答案 0 :(得分: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();
}

非常感谢:)