Java Reference
In-Depth Information
RadioButton radioButton2 = new RadioButton("RadioButton2");
radioButton2.setToggleGroup(radioToggleGroup);
HBox radioBox = new HBox(10, radioButton1, radioButton2);
...
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");
}
});
Because the RadioButton class extends the ToggleButton class, the code in the preceding snippet is very similar
to the code in the section “Using Toggle Groups” earlier in this chapter. Please review that section if you'd like an
explanation of the code in this snippet.
Creating a Hyperlink
The Hyperlink control is a button that has the appearance of a link seen in a browser. It can have a graphic, text, or
both, and it responds to mouse rollovers and clicks. In Step 19 of the previous exercise, you clicked the Hyperlink
control shown in Figure 6-9 . As a result, a message was printed to the Java console indicating that it was clicked. The
following code snippet from Listing 6-12 implements this functionality in the StarterApp program:
Hyperlink link = new Hyperlink("Hyperlink");
link.setOnAction(e -> System.out.println(e.getEventType() + " occurred on Hyperlink"));
Defining a ChoiceBox
When clicked, a ChoiceBox control presents a popup containing a list of items from which to choose. In Step 20 of
the previous exercise, you clicked the ChoiceBox control shown in Figure 6-9 . As a result, a message was printed to
the Java console indicating which item you chose. The following code snippet from Listing 6-12 implements this
functionality in the StarterApp program:
ChoiceBox choiceBox;
choiceBox = new ChoiceBox(model.choiceBoxItems);
choiceBox.getSelectionModel().selectFirst();
choiceBox.getSelectionModel().selectedItemProperty()
.addListener((observable, oldValue, newValue) -> {
System.out.println(newValue + " chosen in ChoiceBox");
});
To initially select the first item in the ChoiceBox , the preceding snippet invokes the selectFirst() method of the
choice box's selectionModel . To detect when the user chooses an item in the ChoiceBox , we add the ChangeListener
shown in the snippet to the selectedItem property of the choice box's selection model.
 
Search WWH ::




Custom Search