AgostinoX
AgostinoX

Reputation: 7683

JavaFX connecting components to model

I have a JavaFX user interface with several controls; the values should be stored inside fields of a Model class; the UI class has a reference to Model.

Say the Model class is the basic:

public static class Model{String myText; /*javabeans getters and setters provided too*/}

The JavaFX Application is the following.

public class T08 extends Application {

Model model;

@Override
public void start(Stage primaryStage) throws Exception {

    model = new Model();

    BorderPane bp = new BorderPane();
    primaryStage.setScene(new Scene(bp));

    //this is the component that should be connected to model.myText
    TextField textField = new TextField();

    bp.setCenter(textField);        
    primaryStage.show();

}

Question

  1. The user can write text in textField control and the text should be saved into model.myText.
  2. During application startup i need to load the data into the Model and have it rendered to the controls.
    I've tried with JavaFX 2.x bindings, but they seem to focus on unidirectional connections. What are my options to accomplish this in a neat way?

Upvotes: 3

Views: 3611

Answers (3)

Daniel Ludwig
Daniel Ludwig

Reputation: 89

Maybe you take a look at https://github.com/laubfall/modelfx. I wrote this library exactly for this reason. It supports bidirectional Bindings for JavaFX-Components (that are composed in a hierarchy) and Properties that resides in a Bean. For further Documentation refer to the Wiki-Pages of the Github-Project modelfx. Hope you find it helpful!

ps: the core functionality uses Bindings.bindBidrectional of JavaFX.

Upvotes: 0

Uluk Biy
Uluk Biy

Reputation: 49185

Ok I wrote some SSCCE sample code, since code explains the things more precisely :)

First example is for this question:

import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.TextField;
import javafx.scene.layout.BorderPane;
import javafx.stage.Stage;

public class JavaFXApplication10 extends Application {

    private Model model = new Model();

    @Override
    public void start(Stage primaryStage) {

        final TextField textField = new TextField();
        textField.setText(model.getMyText());

        Button btn = new Button();
        btn.setText("Done");
        btn.setOnAction(new EventHandler<ActionEvent>() {

            @Override
            public void handle(ActionEvent event) {
                model.setMyText(textField.getText());
                System.out.println("Done.");
                System.out.println("New value: " + model.getMyText());
            }
        });

        BorderPane root = new BorderPane();
        root.setTop(textField);
        root.setBottom(btn);
        primaryStage.setScene(new Scene(root, 300, 250));
        primaryStage.show();
    }

    public static void main(String[] args) {
        launch(args);
    }

    // Data Model
    public static class Model {
        private String myText = "model myText default";

        public String getMyText() {
            return myText;
        }

        public void setMyText(String myText) {
            this.myText = myText;
        }
    }
}

The second modified version (uses bidirectional binding) of this example is here.
In both examples try to click the button.

Upvotes: 3

Puce
Puce

Reputation: 38122

One way is to use myTextProperty.bindBidirectional() instead of myTextProperty.bind(), AFAIK.

Upvotes: 4

Related Questions