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

Updated on: 18-May-2020

491 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements