javauser-interfacejtextfieldclasscastexceptionbeans-binding

ClassCastException Beansbinding Java


Can you tell me why I get ClassCastException exception here?

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

this is the textfield:

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

and the Apprentice.java has the attributee "vorname" (german for firstname) which is private field with getters and setters.

private String vorname;

public Apprentice(){
    ;
}

public String getVorname() {
    return vorname;
}

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

Solution

  • I found the solution by myself, I bound the "Self Object" instead of the "text" is has to look like this:

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

    Thanks anyway :)