Overview of MenuBar, Menu, and MenuItem
Before you can create a menu,
you need to know some specifics about MenuBar,
Menu, and MenuItem. These form the minimum set of classes needed to construct
a main menu for an application. MenuItems are also used by context
(i.e., popup) menus. Thus, these classes form the foundation of the menu
MenuBar is essentially a container for menus. It is the control that
supplies the main menu of an application.
Like all JavaFX controls, it inherits Node.
Thus, it can be added to a scene graph. MenuBar
has only one constructor, which is the default constructor. Therefore,
initially, the menu bar will be empty, and you will need to populate it with
menus prior to use. As a general rule, an application has one and only one menu
MenuBar defines several methods, but often you will use only one: getMenus( ). It returns a list of the menus managed by the
menu bar. It is to this list that you will add the menus that you create. The getMenus( ) method is shown here:
ObservableList<Menu> getMenus( )
A Menu instance is added to this list of menus by calling add( ). You can also use addAll( ) to add two or more Menu instances in a single call. The
added menus are positioned in the bar from left to right, in the order in which
they are added. If you want to add a menu at a specific location, then use this
version of add( ):
void add(int idx, Menu menu)
Here, menu is added at the index specified by idx. Indexing begins at 0, with 0 being the left-most menu.
In some cases, you might want
to remove a menu that is no longer needed. You can do this by calling remove( ) on the ObservableList returned by getMenus(
). Here are two of its forms:
void remove(Menu menu) void remove(int idx)
Here, menu is a reference to the menu to remove, and idx is the index of the menu to remove. Indexing begins at zero.
It is sometimes useful to
obtain a count of the number of items in a menu bar. To do this, call size( ) on the list returned by getMenus( ).
Once a menu bar has been
created and populated, it is added to the scene graph in the normal way.
Menu encapsulates a menu, which is populated with MenuItems. As mentioned,
Menu is derived from MenuItem. This means that one Menu can be a selection in another Menu. This enables one menu to be
submenu of another. Menu defines
Perhaps the most commonly
used is shown here: Menu(String name)
It creates a menu that has
the name specified by name. You can specify
an image along with text with this constructor:
Menu(String name, Node image)
Here, image specifies the image that is displayed. In all cases, the menu
is empty until menu items are added to it. Finally, you don’t have to give a
menu a name when it is constructed. To create an unnamed menu, you can use the
In this case, you can add a
name and/or image after the fact by calling setText( ) or setGraphic( ).
Each menu maintains a list of
menu items that it contains. To add an item to the menu, add items to this
list. To do so, first call getItems( ),
ObservableList<MenuItem> getItems( )
It returns the list of items
currently associated with the menu. To this list, add menu items by calling
either add( ) or addAll( ). Among other actions, you can
remove an item by calling remove( )
and obtain the size of the list by calling size(
One other point: You can add
a menu separator to the list of menu items, which is an object of type SeparatorMenuItem. Separators help
organize long menus by allowing you to group related items together. A
separator can also help set off an important item, such as the Exit selection
in a menu.
MenuItem encapsulates an element in a menu. This element can be either a selection
linked to some program action, such
as Save or Close, or it can cause a submenu to be displayed. MenuItem defines the following three
MenuItem( ) MenuItem(String name)
MenuItem(String name, Node image)
The first creates an empty menu
item. The second lets you specify the name of the item, and the third enables
you to include an image.
A MenuItem generates an action event when selected. You can register
an action event handler for such an event by calling setOnAction( ), just as you did when handling button events. It is
shown again for your convenience:
Here, handler specifies the event handler. You can fire an action event
on a menu item by calling fire( ).
MenuItem defines several methods. One that is often useful is setDisable( ), which you can use to enable or disable a menu
item. It is shown here:
final void setDisable(boolean
If disable is true, the
menu item is disabled and cannot be selected. If disable is false, the
item is enabled. Using setDisable( ),
you can turn menu items on or off, depending on program conditions.