JavaFX example to set action (behavior) to the menu item


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

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.

Setting action to MenuItem

The MenuItem class inherits a property named onAction from the javafx.scene.control.ButtonBase class, which is of the type ObjectProperty<EventHandler<ActionEvent>>. This property represents the action that is invoked whenever you press the button. You can set the value to this property using the setOnAction() method.

One of the ways to set an action to an item menu is using the OnAction() method.

Example

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
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.Image;
import javafx.scene.image.ImageView;
import javafx.scene.paint.Color;
import javafx.stage.FileChooser;
import javafx.stage.Stage;
import javafx.stage.FileChooser.ExtensionFilter;
public class MenuActionExample extends Application {
   public void start(Stage stage) {
      //Creating a menu
      Menu fileMenu = new Menu("File");
      //Creating menu Items
      MenuItem item1 = new MenuItem("Open Image");
      MenuItem item2 = new MenuItem("Clear");
      //Adding all the menu items to the menu
      fileMenu.getItems().addAll(item1, item2);
      //Creating a File chooser
      FileChooser fileChooser = new FileChooser();
      fileChooser.setTitle("Open Image");
      fileChooser.getExtensionFilters().addAll(new ExtensionFilter("All Files", "*.*"));
      //Creating the image view
      ImageView imageView = new ImageView();
      //Setting the image view parameters
      imageView.setX(10);
      imageView.setY(45);
      imageView.setFitWidth(575);
      imageView.setFitHeight(300);
      imageView.setPreserveRatio(true);
      //Handling the event of the Open Image
      item1.setOnAction(new EventHandler<ActionEvent>() {
         public void handle(ActionEvent event) {
            try {
               //File file= fileChooser.showOpenDialog(stage);
               File file = fileChooser.showOpenDialog(stage);
               InputStream stream = new FileInputStream(file);
               Image image = new Image(stream);
               //Setting image to the image view
               imageView.setImage(image);
            }
            catch (FileNotFoundException e) {
               e.printStackTrace();
            }
         }
      });
      //Handling the event of the exit menu item
      item2.setOnAction(event -> {
         imageView.setImage(null);
      });
      //Creating a menu bar and adding menu to it.
      MenuBar menuBar = new MenuBar(fileMenu);
      menuBar.setTranslateX(3);
      menuBar.setTranslateY(3);
      //Setting the stage
      Group root = new Group(menuBar, imageView);
      Scene scene = new Scene(root, 595, 355, Color.BEIGE);
      stage.setTitle("Menu Example");
      stage.setScene(scene);
      stage.show();
   }
   public static void main(String args[]){
      launch(args);
   }
}

Output

Updated on: 18-May-2020

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements