2

你能告诉我为什么我在这里得到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”(德语为名字),它是带有 getter 和 setter 的私有字段。

private String vorname;

public Apprentice(){
    ;
}

public String getVorname() {
    return vorname;
}

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

1 回答 1

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();
}

不管怎么说,还是要谢谢你 :)

于 2012-11-21T19:39:26.347 回答