Node createScrollMiscDemoNode() {
final ToggleGroup radioToggleGroup = new ToggleGroup();
ChoiceBox choiceBox;
final TextField textField;
final PasswordField passwordField;
final TextArea textArea;
Slider slider;
ProgressIndicator progressIndicator;
ProgressBar progressBar;
ScrollBar scrollBar;
Button button = new Button("Button");
button.setOnAction(e -> System.out.println(e.getEventType() + " occurred on Button"));
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());
});
RadioButton radioButton1 = new RadioButton("RadioButton1");
radioButton1.setToggleGroup(radioToggleGroup);
RadioButton radioButton2 = new RadioButton("RadioButton2");
radioButton2.setToggleGroup(radioToggleGroup);
HBox radioBox = new HBox(10, radioButton1, radioButton2);
Hyperlink link = new Hyperlink("Hyperlink");
link.setOnAction(e -> System.out.println(e.getEventType() + " occurred on Hyperlink"));
choiceBox = new ChoiceBox(model.choiceBoxItems);
MenuItem menuA = new MenuItem("MenuItem A");
menuA.setOnAction(e -> System.out.println(e.getEventType() + " occurred on Menu Item A"));
MenuItem menuB = new MenuItem("MenuItem B");
MenuButton menuButton = new MenuButton("MenuButton");
menuButton.getItems().addAll(menuA, menuB);
MenuItem splitMenuA = new MenuItem("MenuItem A");
splitMenuA.setOnAction(e -> System.out.println(e.getEventType()
+ " occurred on Menu Item A"));
MenuItem splitMenuB = new MenuItem("MenuItem B");
SplitMenuButton splitMenuButton = new SplitMenuButton(splitMenuA, splitMenuB);
splitMenuButton.setText("SplitMenuButton");
splitMenuButton.setOnAction(e -> System.out.println(e.getEventType()
+ " occurred on SplitMenuButton"));
textField = new TextField();
textField.setPromptText("Enter user name");
textField.setPrefColumnCount(16);
passwordField = new PasswordField();
passwordField.setPromptText("Enter password");
passwordField.setPrefColumnCount(16);
textArea = new TextArea();
textArea.setPrefColumnCount(12);
textArea.setPrefRowCount(4);
LocalDate today = LocalDate.now();
DatePicker datePicker = new DatePicker(today);
datePicker.setOnAction(e -> System.out.println("Selected date: " + datePicker.getValue()));
ColorPicker colorPicker = new ColorPicker(Color.BLUEVIOLET);
colorPicker.setOnAction(e -> System.out.println("Selected color: " + colorPicker.getValue()));
progressIndicator = new ProgressIndicator();
progressIndicator.setPrefWidth(200);
slider = new Slider(-1, model.maxRpm, 0);
slider.setPrefWidth(200);
progressBar = new ProgressBar();
progressBar.setPrefWidth(200);
scrollBar = new ScrollBar();
scrollBar.setPrefWidth(200);
scrollBar.setMin(-1);
scrollBar.setMax(model.maxKph);
VBox variousControls = new VBox(20,
button,
checkBox,
radioBox,
link,
choiceBox,
menuButton,
splitMenuButton,
textField,
passwordField,
new HBox(10, new Label("TextArea:"), textArea),
datePicker, colorPicker,
progressIndicator, slider,
progressBar, scrollBar);
variousControls.setPadding(new Insets(10, 10, 10, 10));
radioToggleGroup.selectToggle(radioToggleGroup.getToggles().get(0));
radioToggleGroup.selectedToggleProperty().addListener((ov, oldValue, newValue) -> {
RadioButton rb = ((RadioButton) radioToggleGroup.getSelectedToggle());
if (rb != null) {
System.out.println(rb.getText() + " selected");
}
});
textField.textProperty().addListener((ov, oldValue, newValue) -> {
System.out.println("TextField text is: " + textField.getText());
});
passwordField.focusedProperty().addListener((ov, oldValue, newValue) -> {
if (!passwordField.isFocused()) {
System.out.println("PasswordField text is: "
+ passwordField.getText());
}
});
textArea.focusedProperty().addListener((ov, oldValue, newValue) -> {
if (!textArea.isFocused()) {
System.out.println("TextArea text is: " + textArea.getText());
}
});
slider.valueProperty().bindBidirectional(model.rpm);
progressIndicator.progressProperty().bind(model.rpm.divide(model.maxRpm));
scrollBar.valueProperty().bindBidirectional(model.kph);
progressBar.progressProperty().bind(model.kph.divide(model.maxKph));
choiceBox.getSelectionModel().selectFirst();
choiceBox.getSelectionModel().selectedItemProperty()
.addListener((observable, oldValue, newValue) -> {
System.out.println(newValue + " chosen in ChoiceBox");
});
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());
}
});
MenuItem contextA = new MenuItem("MenuItem A");
contextA.setOnAction(e -> System.out.println(e.getEventType()
+ " occurred on Menu Item A"));
MenuItem contextB = new MenuItem("MenuItem B");
contextMenu = new ContextMenu(contextA, contextB);
return scrollPane;
}
Defining a ScrollPane
As you experienced in Step 22 of the previous exercise, the ScrollPane shown in Figures 6-9 and 6-10 contains several UI controls, and has a vertical scrollbar so that all of the controls may be accessed.
The code from the StarterApp program that defines the ScrollPane and populates it with UI controls is shown in Listing 6-12.
Listing 6-12. The createScrollMiscDemoNode() Method
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.