Introduction to Java Programming and
Data Structures
Twelfth Edition
Chapter 31
Advanced JavaFX
(part-3) Menu and
SplitPane
Copyright © 2020 Pearson Education, Inc. All Rights Reserved
Objectives
31.5 To create menus using the Menu, MenuItem, CheckMenuItem, and RadioMemuItem
classes (§31.6).
31.6 To create context menus using the ContextMenu class (§31.7).
31.7 To use SplitPane to create adjustable horizontal and vertical panes (§31.8).
Copyright © 2020 Pearson Education, Inc. All Rights Reserved
Menu
Menus make selection easier and are widely used in
window applications. JavaFX provides five classes that
implement menus: MenuBar, Menu, MenuItem,
CheckMenuItem, and RadioButtonMenuItem.
MenuBar is a top-level menu component used to hold the
menus. A menu consists of menu items that the user can
select (or toggle on or off). A menu item can be an instance
of MenuItem, CheckMenuItem, or RadioButtonMenuItem.
Menu items can be associated with nodes and keyboard
accelerators.
Copyright © 2020 Pearson Education, Inc. All Rights Reserved
MenuBar and MenuItem
MenuBar is a top-level menu component used to hold the
menus. A menu consists of menu items
MenuExample
You need to uncomment each section for coming slides
Copyright © 2020 Pearson Education, Inc. All Rights Reserved
SubMenu
Creating submenu items. You can also embed menus
inside menus so the embedded menus become submenus.
Copyright © 2020 Pearson Education, Inc. All Rights Reserved
Check-Box Menu items
Creating submenu items. You can also embed menus
inside menus so the embedded menus become submenus.
Copyright © 2020 Pearson Education, Inc. All Rights Reserved
RadioMenuItem
You can also add radio menu items to a menu, using the
RadioMenuItem class. This is often useful when you have a
group of mutually exclusive choices in the menu
Copyright © 2020 Pearson Education, Inc. All Rights Reserved
Image Icons and Keyboard Accelerators
The MenuItem has a graphic property for specifying a node
to be displayed in the menu item. Usually, the graphic is an
image view.
A key accelerator lets you select a menu item directly by pressing the CTRL and the accelerator key.
Copyright © 2020 Pearson Education, Inc. All Rights Reserved
Creating Menus
MenuDemo
Copyright © 2020 Pearson Education, Inc. All Rights Reserved
Context Menu
A context menu, also known as a popup menu, is like a
regular menu, but does not have a menu bar and can float
anywhere on the screen. Creating a context menu is similar
to creating a regular menu. First, you create an instance of
ContextMenu, then you can add MenuItem,
CheckMenuItem, and RadioMenuItem to the context menu.
Copyright © 2020 Pearson Education, Inc. All Rights Reserved
Creating Context Menus
ContextMenuDemo
Copyright © 2020 Pearson Education, Inc. All Rights Reserved
SplitPane
The SplitPane class can be used to display multiple panes
and allow the user to adjust the size of the panes.
Copyright © 2020 Pearson Education, Inc. All Rights Reserved
Using SplitPane
[Link] The getter and setter methods for property values and
a getter for property itself are provided in the class, but
omitted in the UML diagram for brevity.
[Link]
-orientation: ObjectProperty<Orientation> Specifies the orientation of the pane.
+SplitPane() Constructs a default split pane with horizontal orientation.
+getItems(): Returns a list of items in the pane.
SplitPaneDemo
Copyright © 2020 Pearson Education, Inc. All Rights Reserved
Copyright
This work is protected by United States copyright laws and is
provided solely for the use of instructors in teaching their
courses and assessing student learning. Dissemination or sale of
any part of this work (including on the World Wide Web) will
destroy the integrity of the work and is not permitted. The work
and materials from it should never be made available to students
except by instructors using the accompanying text in their
classes. All recipients of this work are expected to abide by these
restrictions and to honor the intended pedagogical purposes and
the needs of other instructors who rely on these materials.
Copyright © 2020 Pearson Education, Inc. All Rights Reserved