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:10.1.10.12:1521:orcl" 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 |
@FXML
ChoiceBox<String> cbDriver;
@FXML
TextField tfDBHost, tfDBPort, tfDBSID, tfDBURL;
@FXML
public void initialize() {
cbDriver.getItems().addAll( "thin", "oci");
cbDriver.setValue("thin");
// ex, jdbc:oracle:thin:@localhost:1521:orcl
tfDBURL.textProperty().bind(
Bindings.concat(
"jdbc:oracle:",
cbDriver.valueProperty(),
":",
tfDBHost.textProperty(),
":",
tfDBPort.textProperty(),
":",
tfDBSID.textProperty()
));
}
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