Java Reference
In-Depth Information
// Alarm
Menu alarm = new Menu("Alarm");
ToggleGroup tGroup = new ToggleGroup();
RadioMenuItem soundAlarmItem = new RadioMenuItem();
soundAlarmItem.setToggleGroup(tGroup);
soundAlarmItem.setText("Sound Alarm");
RadioMenuItem stopAlarmItem = new RadioMenuItem();
stopAlarmItem.setToggleGroup(tGroup);
stopAlarmItem.setText("Alarm Off");
stopAlarmItem.setSelected(true);
alarm.getItems().add(soundAlarmItem);
alarm.getItems().add(stopAlarmItem);
At times you may want to separate menu items with a visual line separator. To cre-
ate a visual separator, create an instance of a SeparatorMenuItem class to be ad-
ded to a menu via the getItems() method. The method getItems() returns an
observable list of MenuItem objects ( ObservableList<MenuItem> ). As you
will see in Recipe 14-10, you can be notified when items in a collection are altered.
The following code line adds a visual line separator ( SeparatorMenuItem ) to the
menu:
menu.getItems().add(new SeparatorMenuItem());
Other menu items used are the check menu item ( CheckMenuItem ) and the radio
menu item ( RadioMenuItem ), and they are similar to their counterparts in JavaFX
UI controls check box ( CheckBox ) and radio button ( RadioButton ), respectively.
Prior to adding the menu bar to the scene, you will notice the bound property
between the preferred width of the menu bar and the width of the Stage object via the
bind() method. When binding these properties you will see the menu bar's width
stretch when the user resizes the screen. You will see how binding works in Recipe
14-9, “Binding Expressions.” This code snippet shows the binding between the menu
bar's width property and the stage's width property.
Search WWH ::




Custom Search