S
S
Sergey Sidorov2013-06-23 10:13:55
Java
Sergey Sidorov, 2013-06-23 10:13:55

How to make data binding in JavaFX 2?

I rarely work with the client-side, maybe that's why such a problem arose.
So, what data binding possibilities I found in java fx:
1. Through ChangeListener:

    @FXML
    private TextField firstNameField;

    private Person person;

    public void initialize(final URL arg0, final ResourceBundle arg1) {
        firstNameField.textProperty().addListener(new ChangeListener<String>() {
            @Override
            public void changed(final ObservableValue<? extends String> paramObservableValue,
                    final String paramT1, final String paramT2) {
                person.setFirstName(paramT2);
            }
        });
    }


In this case, we get a cumbersome and useless code + one-way communication (we listen only to changes from the UI)

2. Through properties:

    @FXML
    private TextField firstNameField;
    private final StringProperty firstName = new SimpleStringProperty(this, "firstName");

    public void initialize(final URL arg0, final ResourceBundle arg1) {
        Bindings.bindBidirectional(firstNameField.textProperty(), firstName);
    }


Everything is beautiful here. But! I have a Person POJO object, how will this value get into it? Those. Do I need to change the values ​​for each field of each model, first in one direction (to display the fields of the model), then in the other (to make changes to the model)? What is the point then, if with the same success I can pull out the values ​​from the text fields in the same way (especially since they must also be in the controller to do the binding)?

3. Push properties into the model class, and then as in #2.

public class Person {
    private StringProperty firstName = new SimpleStringProperty(this, "firstName", "");
    public final String getFirstName() { return firstName.get(); }
    public final void setFirstName(String value) { firstName.set(value); }
    public final StringProperty firstNameProperty() { return firstName; }
}


This solves the problem described in #2, but at the same time it corrupts the model class ... I would like to leave them as POJOs.

What I would like to see: a model object comes to the controller, we shove it into some kind of wrapper that binds this model to TextFields and other controls. Even if the binding needs to be explicitly declared:

    public void initialize(final URL arg0, final ResourceBundle arg1) {
        Bindings.bindBidirectional(firstNameField.textProperty(), personWrapper.getProperty("firstName"));
    }


Maybe there is something similar?

A separate question arose about mapping a field with an enum type to a group of RadioButtons... I couldn't do it without dancing with a tambourine... Although, it would seem, everything is quite simple. Make EnumProperty and manage everything through ToggleGroup... And I would like to have such a tool out of the box, and not write bicycles...

I would be grateful for any constructive answers.

Colleagues, if you are already minus, at least explain what is the reason. What would be the opportunity to fix the jambs. Thank you.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Andrey Tolstoy, 2015-02-07
@anzood

Look here
https://ugate.wordpress.com/2012/06/06/javafx-pojo...
https://ugate.wordpress.com/2012/06/14/javafx-prog...
https:// ugate.wordpress.com/2012/07/30/javafx-prog...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question