Java Reference
In-Depth Information
As shown in the following snippet from Listing 6-12, the
content
of a
ScrollPane
is a
Node
subclass, in this case
a
VBox
that contains several nodes. When the contents are larger than the viewable area of the
ScrollPane
, horizontal
and/or vertical scrollbars appear according to the specified
hbarPolicy
and
vbarPolicy
.
ScrollPane scrollPane = new ScrollPane(variousControls);
scrollPane.setHbarPolicy(ScrollPane.ScrollBarPolicy.NEVER);
scrollPane.setVbarPolicy(ScrollPane.ScrollBarPolicy.AS_NEEDED);
scrollPane.setOnMousePressed((MouseEvent me) -> {
if (me.getButton() == MouseButton.SECONDARY) {
contextMenu.show(stage, me.getScreenX(), me.getScreenY());
}
});
Other useful
ScrollPane
properties include:
•
pannable
, which enables the user to pan the contents of the
ScrollPane
by dragging it with
the mouse
fitToWidth/fitToHeight
, which causes the
content
node (if resizable) to be stretched to fit
the width/height of the
ScrollPane
Note that we're using an
onMousePressed()
event handler in the previous snippet. We walk through that
functionality after discussing some of the UI controls that are contained within our
ScrollPane
, beginning with
the
CheckBox
.
•
Using a
CheckBox
As you experienced in Step 17 of the exercise, the
ScrollPane
shown in Figure
6-9
contains a
CheckBox
.
When the
CheckBox
is clicked, a message is printed to the Java console indicating the state of its
selected
property.
The following code snippet, from Listing 6-12, implements this functionality in the StarterApp program:
final CheckBox checkBox = new CheckBox("CheckBox");
checkBox.setOnAction(e -> {
System.out.print(e.getEventType() + " occurred on CheckBox");
System.out.print(", and selectedProperty is: ");
System.out.println(checkBox.selectedProperty().getValue());
});
A
CheckBox
may also represent a third
indeterminate
state by setting its
allowIndeterminate
property to
true. This third state is typically represented in the
CheckBox
with a dash, and is useful for indicating that the state
represented by the
CheckBox
is unknown.
Defining a
RadioButton
In Step 18 of the previous exercise, you selected each of the
RadioButton
controls shown in Figure
6-9
. As a result,
a message was printed to the Java console indicating which
RadioButton
was selected. The following code snippet,
from Listing 6-12, implements this functionality in the StarterApp program:
final ToggleGroup radioToggleGroup = new ToggleGroup();
RadioButton radioButton1 = new RadioButton("RadioButton1");
radioButton1.setToggleGroup(radioToggleGroup);
Search WWH ::
Custom Search