How to add accelerators to a menu item?

JavaFXObject Oriented ProgrammingProgramming

A menu is a list of options or commands presented to the user, typically menus contains items that perform some action. The contents of a menu are known as menu items and a menu bar holds multiple menus.

In JavaFX a menu is represented by the javafx.scene.control.Menu class, a menu item is represented by the javafx.scene.control.MenuItem class, And, the javafx.scene.control.MenuBar class represents a menu bar.

Adding accelerators to a menu item −

Accelerators are short cuts to menu Items. The MenuItem class contains a property named accelerator (of type KeyCombination), which associates a combination to the action of the current MenuItem.

You can set value to this property using the setAccelerator() method. Therefore, to set accelerator to a particular MenuItem, invoke the setAccelerator() method on it as shown below −

MenuItem item = new MenuItem("Exit", imgView3);
item.setAccelerator(KeyCombination.keyCombination("Ctrl+X"));

Example

import javafx.application.Application;
import javafx.event.ActionEvent;
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.image.ImageView;
import javafx.scene.input.KeyCombination;
import javafx.scene.paint.Color;
import javafx.stage.Stage;
public class MenuItemAccelerators extends Application {
   @Override
   public void start(Stage stage) {
      //Creating image view files
      ImageView imgView1 = new ImageView("UIControls/open.png");
      imgView1.setFitWidth(20);
      imgView1.setFitHeight(20);
      ImageView imgView2 = new ImageView("UIControls/Save.png");
      imgView2.setFitWidth(20);
      imgView2.setFitHeight(20);
      ImageView imgView3 = new ImageView("UIControls/Exit.png");
      imgView3.setFitWidth(20);
      imgView3.setFitHeight(20);
      //Creating menu
      Menu fileMenu = new Menu("File");
      //Creating menu Items
      MenuItem item1 = new MenuItem("Open File", imgView1);
      MenuItem item2 = new MenuItem("Save file", imgView2);
      MenuItem item3 = new MenuItem("Exit", imgView3);
      //Setting accelerators to the menu items
      item1.setAccelerator(KeyCombination.keyCombination("Ctrl+F"));
      item2.setAccelerator(KeyCombination.keyCombination("Ctrl+S"));
      item3.setAccelerator(KeyCombination.keyCombination("Ctrl+X"));
      //Adding all the menu items to the menu
      fileMenu.getItems().addAll(item1, item2, item3);
      //Creating a menu bar and adding menu to it.
      MenuBar menuBar = new MenuBar(fileMenu);
      menuBar.setTranslateX(200);
      menuBar.setTranslateY(20);
      //Setting the stage
      Group root = new Group(menuBar);
      Scene scene = new Scene(root, 595, 200, Color.BEIGE);
      stage.setTitle("Menu Example");
      stage.setScene(scene);
      stage.show();
   }
   public static void main(String args[]){
      launch(args);
   }
}

Output

raja
Published on 20-May-2020 10:45:47
Advertisements