Featured Post

Applying Email Validation to a JavaFX TextField Using Binding

This example uses the same controller as in a previous post but adds a use case to support email validation.  A Commons Validator object is ...

Monday, May 2, 2016

Creating JavaFX Derived Fields with Bindings.concat()

This post shows how to connect a ChoiceBox and several TextFields in JavaFX to produce a derived value.  The form requires a database URL which the user enters in parts: driver type, host, port, and SID.  As the component controls are being edited by the user, the DB URL TextField is immediately updated.

This is done with a single JavaFX call that uses the Bindings.concat() method to link up a mixture of constants and dynamic values.

The code for this example can be found on GitHub.  Look for the package "derived".

This video demonstrates the application.  The URL "jdbc:oracle:thin:" is constructed then later changed to "jdbc:oracle:oci:localhost:1521:orcl".

This is the SceneBuilder definition of the fields.  There is a ChoiceBox and four TextFields.  The DB URL TextField is not editable and is grayed out with a style.

Derived Fields Example in Scene Builder
The class referenced in Scene Builder, "DerivedControl" contains these fields and this @FXML initialize() method.

ChoiceBox<String> cbDriver;

TextField tfDBHost, tfDBPort, tfDBSID, tfDBURL;

public void initialize() {
    cbDriver.getItems().addAll( "thin", "oci");

    // ex, jdbc:oracle:thin:@localhost:1521:orcl


This is the varargs form of Bindings.concat().  Several of the elements are constants such as the prefix "jdbc:oracle" and the colon delimiters.  For the dynamic piece, I bound to the text and value properties of the other components.  I bind to valueProperty() for the ChoiceBox. and textProperty() for the TextFields.  The sequence of these objects produces a formatted Oracle JDBC URL.

While it's not difficult to put a handler on each of these fields as in Swing, this functionality is much less fragmented and the statement more readable.  This construct puts a lot of the burden on the UI updates on JavaFX which is where it belongs.

No comments:

Post a Comment