Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?
like image 810
AgostinoX Avatar asked Dec 10 '25 09:12

AgostinoX


1 Answers

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

like image 146
Puce Avatar answered Dec 12 '25 22:12

Puce



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!