Java Reference
In-Depth Information
gridLinesToggle.setText(newVal ? "On" : "Off");
});
Next, you apply a change listener to a slider control that allows the user to adjust
the target grid pane's top padding. To create a change listener for a slider, you instanti-
ate a ChangeListener<Number> . Again, you'll use a lambda expression with a
signature the same as its formal type parameter Number . When a change occurs, the
slider's value is used to create an Insets object, which becomes the new padding for
the target grid pane. Shown here is the change listener for the top padding and slider
control:
gridPaddingSlider.valueProperty().addListener((
ObservableValue<? extends Number> ov, Number
oldVal, Number newVal) -> {
double top1 = targetGridPane.getInsets().getTop();
double right1 = targetGridPane.getInsets().getRight();
double bottom1
= targetGridPane.getInsets().getBottom();
double left1 = targetGridPane.getInsets().getLeft();
Insets newInsets = new Insets((double) newVal,
right1, bottom1, left1);
targetGridPane.setPadding(newInsets);
});
Because the implementation of the other slider controls that handle left padding,
horizontal gap, and vertical gap are virtually identical to the top padding slider control
mentioned previously, you can fast-forward to cell constraints controls.
The last bits of grid control panel properties that you want to manipulate are the tar-
get grid pane's cell constraints. For brevity, the example only allows the user to set a
component's alignment inside of a cell of a GridPane . To see more properties to
modify, refer to the Javadoc on javafx.scene.layout.GridPane . Figure 15-8
depicts the cell constraint settings for individual cells. An example is to left-justify the
label Age on the target grid pane. Because cells are zero-relative, you will enter 0 in
the Cell Column field and 2 into the Cell Row field. Next, you select the drop-down
box Horiz. Align to LEFT. Once you're satisfied with the settings, click Apply. Figure
15-9 shows the Age label control left-aligned horizontally. To implement this change,
create a lambda expression that implements EventHandler<ActionEvent> for
the apply button's onAction attribute. Inside of the lambda expression, you iterate
 
 
Search WWH ::




Custom Search