Java Reference
In-Depth Information
TextField ageFld = new TextField();
Button saveButt = new Button("Save");
// First name label
GridPane.setHalignment(fNameLbl, HPos.RIGHT);
add(fNameLbl, 0, 0);
//... The rest of the form code
To manipulate the target form you need to create a grid property control panel
( GridPaneControlPanel ). This class is responsible for binding the target form's
grid pane properties to UI controls that allow users to adjust values using the keyboard
and mouse. As you learned in Chapter 14 , in Recipe 14-10, you can bind values with
JavaFX properties. But instead of binding values directly, you can also be notified
when a property has changed.
Another feature that you can add to properties is the change listener. JavaFX
javafx.beans.value.ChangeListener s are similar to Java swing's property
change support ( java.beans.PropertyChangeListener ). Similarly, when a
bean's property value has changed, you will want to be notified. Change listeners are
designed to intercept the change by making the old and new value available to the de-
veloper. The example starts this process by creating a JavaFXchange listener for the
toggle button to turn gridlines on or off. When a user interacts with the toggle button,
the change listener will simply update the target's grid pane's gridlinesVisible
property. Because a toggle button's ( ToggleButton ) selected property is a
Boolean value, you instantiate a ChangeListener class with its formal type para-
meter as Boolean . You'll also notice the lambda expression change listener imple-
mentation, where its inbound parameters will match the generic formal type parameter
specified when instantiating a ChangeListener<Boolean> . When a property
change event occurs, the change listener will invoke setGridLinesVisible() on
the target grid pane with the new value and update the toggle button's text. The follow-
ing code snippet shows a ChangeListener<Boolean> added to a ToggleBut-
ton :
gridLinesToggle.selectedProperty().addListener(
(ObservableValue<? extends Boolean> ov,
Boolean oldValue, Boolean newVal) -> {
targetGridPane.setGridLinesVisible(newVal);
Search WWH ::




Custom Search