Menus, Toolbars & Dialogs
Menus, Toolbars & Dialogs
Almost every desktop application provides three tiers of user-facing command surface: a menu bar that exposes the full feature set, a toolbar that puts the most frequent actions one click away, and dialogs that prompt the user before a destructive operation or surface a result. JavaFX ships with first-class controls for all three tiers. This lesson walks through each one in practical depth.
Menu Bar
The JavaFX menu bar is assembled from three classes that mirror the visual hierarchy:
MenuBar— the horizontal bar that sits at the top of the window.Menu— a top-level label (File, Edit, Help …) that opens a drop-down when clicked.MenuItem— a single clickable entry inside that drop-down.
You build the tree in code, attach setOnAction handlers to each MenuItem, and then place the MenuBar into a layout pane — typically the top slot of a BorderPane.
A SeparatorMenuItem renders as a thin horizontal rule, grouping related items visually without adding a clickable entry.
Keyboard Accelerators
Users expect Ctrl+S to save and Ctrl+N to create a new document. Attach a KeyCombination to any MenuItem via setAccelerator(). JavaFX fires the item's action handler when the key combination is pressed regardless of whether the menu is open.
SHORTCUT_DOWN instead of CONTROL_DOWN. On macOS, SHORTCUT_DOWN maps to the Command key; on Windows and Linux it maps to Ctrl. Using it makes your app feel native on every platform without any conditional code.
CheckMenuItem and RadioMenuItem
Beyond plain items, JavaFX provides two stateful variants that are common in View or Options menus:
CheckMenuItem— shows a check mark when selected. Toggle withsetSelected(); read state withisSelected().RadioMenuItem— part of aToggleGroupso that only one is selected at a time, exactly like radio buttons.
Toolbar
A ToolBar is a horizontal (or vertical) strip of buttons that give users one-click access to the most common actions. Like the menu bar, it belongs in the top region of a BorderPane, placed just below the menu bar using a VBox wrapper.
saveBtn.setTooltip(new Tooltip("Save (Ctrl+S)"));. The platform shows it automatically on hover.
Combine the menu bar and toolbar in a VBox so they stack neatly at the top:
Alert Dialogs
JavaFX's Alert class wraps the standard dialog patterns that every desktop user recognises. You pick an AlertType, set a message, and call showAndWait(). The call blocks on the JavaFX Application Thread until the user dismisses the dialog, then returns an Optional<ButtonType> describing which button was pressed.
The five built-in alert types are:
AlertType.INFORMATION— informational message, single OK button.AlertType.WARNING— caution, single OK button.AlertType.ERROR— error report, single OK button.AlertType.CONFIRMATION— OK / Cancel buttons, returns the chosen button.AlertType.NONE— no default buttons; you add your ownButtonTypes.
Custom Button Labels
When the default "OK" and "Cancel" labels do not match your UI copy, replace them with named ButtonType instances:
showAndWait(), not show(), for confirmation dialogs. show() is non-blocking — your code continues immediately after displaying the dialog, so the Optional is empty and you cannot know what the user chose. Reserve show() only for non-blocking informational popups where you do not need the result.
TextInputDialog and ChoiceDialog
Two additional built-in dialogs handle common interaction patterns without requiring you to build a custom layout:
TextInputDialog— shows a text field so the user can enter a string. ReturnsOptional<String>.ChoiceDialog<T>— shows aComboBoxpre-filled with choices you supply. ReturnsOptional<T>.
Putting It Together
In a real application, the menu bar, toolbar, and dialog code belong in the controller class (which you learned in Lesson 7). The FXML file declares the MenuBar and ToolBar nodes; the controller holds the @FXML-injected references and wires up the action handlers. Dialogs are almost always created imperatively in handler methods — they are transient objects not worth declaring in FXML.
Summary
A professional JavaFX application exposes commands through a MenuBar (full feature set), a ToolBar (quick access), and Alert dialogs (user prompts). Use KeyCodeCombination with SHORTCUT_DOWN for cross-platform accelerators. Prefer showAndWait() and inspect the Optional<ButtonType> return value for any dialog that requires a decision. In the next lesson you will build a complete form-based application that uses all the controls and patterns from this tutorial.