- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to add mnemonics to a menu in JavaFX?
A menu is a list of options or commands presented to the user. In JavaFX a menu is represented by the javafx.scene.control.Menu class, you can create a menu by instantiating this class.
Setting mnemonic to a Menu
A mnemonic is a number or character, in the menu title of User interface component (button, text field, etc.) typically with an underscore. If you press this character along with the Alt key the respective menu item will be focused.
You can set a mnemonic to a menu using the setMnemonicParsing() method. Pass the boolean value true as an argument to this method.
To set mnemonic parsing on the menu −
Create menu items by instantiating the MenuItem class
Create a menu by instantiating the Menu class add the above-created menu items to it.
Set the value of the mnemonicParsing property to true using the setMnemonicParsing() method.
Instantiate the MenuBar class bypassing the menus as a parameter to its constructor.
Add the MenuBar to the scene.
Example
import javafx.application.Application; import javafx.scene.Group; import javafx.scene.Scene; import javafx.scene.control.Menu; import javafx.scene.control.MenuBar; import javafx.scene.control.MenuItem; import javafx.scene.paint.Color; import javafx.stage.Stage; public class MenuItem_Mnemonics extends Application { public void start(Stage stage) { //Creating file menu Menu file = new Menu("_File"); file.setMnemonicParsing(true); //Creating file menu items MenuItem item1 = new MenuItem("New"); MenuItem item2 = new MenuItem("Open File"); MenuItem item3 = new MenuItem("Close"); //Adding all the menu items to the file menu file.getItems().addAll(item1, item2, item3); //Creating FileList menu Menu fileList = new Menu("_Edit"); fileList.setMnemonicParsing(true); //Creating fileList menu items MenuItem item6 = new MenuItem("Copy"); MenuItem item7 = new MenuItem("Paste"); MenuItem item8 = new MenuItem("Select All"); //Adding all the items to File List menu fileList.getItems().addAll(item6, item7, item8); //Creating Skin menu Menu skin = new Menu("_Display"); skin.setMnemonicParsing(true); //Creating skin menu items MenuItem item9 = new MenuItem("Regular"); MenuItem item10 = new MenuItem("Inverse"); skin.getItems().addAll(item9, item10); //Adding all elements to Skin menu //Creating a menu bar and adding menu to it. MenuBar menuBar = new MenuBar(); menuBar.setTranslateX(200); menuBar.setTranslateY(20); menuBar.getMenus().addAll(file, fileList, skin); //Setting the stage Group root = new Group(menuBar); Scene scene = new Scene(root, 595, 200, Color.BEIGE); stage.setTitle("Menu Bar Example"); stage.setScene(scene); stage.show(); } public static void main(String args[]){ launch(args); } }
Output
- Related Articles
- How to add a separator to a Menu in JavaFX?
- How to set mnemonics in a label using JavaFX?
- How to add the slider to a menu item in JavaFX?
- How to add image to the menu item in JavaFX?
- How to add context menu to an image in JavaFX?
- How to disable a menu item in JavaFX
- How to create a Menu using JavaFX?
- How to toggle menu items in JavaFX?
- How to add accelerators to a menu item?
- How to add data to a TableView in JavaFX?
- How to add a separator in Menu item in Tkinter?
- How to add custom fonts to a text in JavaFX?
- How to add colors to nodes in JavaFX?
- How to add an image to a button (action) in JavaFX?
- How to add a custom right-click menu to a webpage in JavaScript?
