Java Reference
In-Depth Information
alignToggleGroup.selectToggle(alignToggleGroup.getToggles().get(0));
alignToggleGroup.selectedToggleProperty().addListener((ov, oldValue, newValue) -> {
ToggleButton tb = ((ToggleButton) alignToggleGroup.getSelectedToggle());
if (tb != null) {
System.out.println(tb.getId() + " selected");
}
});
This use case is the classic Left-Alignment button in many document editing applications, where only one of the
Alignment buttons may be selected at any given time. The
ToggleGroup
instance is passed into the
setToggleGroup()
method of the
ToggleButton
shown in the preceding snippet to provide this mutually exclusive behavior.
In addition to providing mutual exclusivity, the
ToggleGroup
instance is used in this snippet for two purposes:
1.
To initially select the first
ToggleButton
in the group, by using the
selectToggle()
method of the
ToggleGroup
instance.
2.
To detect when the currently selected
ToggleButton
changes. This is accomplished by
adding a
ChangeListener
to the
selectedToggle
property of the
ToggleGroup
, and then
using its
getSelectedToggle()
method to ascertain which
ToggleButton
is currently
selected. Note that this is generally preferred over putting an
onAction
event handler in
each of the toggle buttons that are participating in a toggle group.
Inserting a Separator into a Toolbar
It is sometimes useful to visually separate toolbar buttons by using the vertical separators shown in Figure
6-3
.
To accomplish this, use the
Separator
class as shown in this line from Listing 6-4:
new Separator(Orientation.VERTICAL),
Although we didn't make use of separators in the menus of this StarterApp program,
Separator
objects may be
used in menus as well. Of course, separators used in menus typically have a
HORIZONTAL Orientation
.
Creating a
TabPane
and Defining Tabs
One of the principles of UI design is called
progressive disclosure,
which states that a UI should reveal its functionality
progressively rather than inundating the user with all of its functionality at once. The
TabPane
is a good example of
this principle in use, as each tab discloses its functionality while hiding the functionality contained in the other tabs.
To create the
TabPane
instance, our StarterApp program defines a method that we've arbitrarily named
createTabs()
, shown in Listing 6-5. This method leverages the
TabPane
and
Tab
classes, and returns a
TabPane
instance that contains the desired
Tab
objects.
Listing 6-5.
The
createTabs()
Method Located in
StarterAppMain.java
TabPane createTabs() {
final WebView webView = new WebView();
Tab tableTab = new Tab("TableView");
tableTab.setContent(createTableDemoNode());
tableTab.setClosable(false);
Tab accordionTab = new Tab("Accordion/TitledPane");
accordionTab.setContent(createAccordionTitledDemoNode());
accordionTab.setClosable(false);
Search WWH ::
Custom Search