ClassCastException Beansbinding Java

问题描述 投票:1回答:1

你能告诉我为什么我在这里得到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;
}
java user-interface jtextfield classcastexception beans-binding
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();
}

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

© www.soinside.com 2019 - 2024. All rights reserved.