×

JavaFX Menu

In the JavaFX application, in order to create a menu, menu items, and menu bar, Menu, MenuItem, and MenuBar class is used. The menu allows us to choose options among available choices in the application. All methods needed for this purpose are present in the javafx.scene.control.Menu class.

JavaFX Menu:

Example:

import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.stage.Stage;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.control.Menu;
import javafx.scene.control.MenuBar;
import javafx.scene.control.MenuItem;
import javafx.scene.layout.VBox;


  
public class MenuUI extends Application {  


    @Override  
    public void start(Stage primaryStage) throws Exception 
    {  
      Menu newmenu = new Menu("File");
      Menu newmenu2 = new Menu("Edit");


          MenuItem m1 = new MenuItem("Open");
          MenuItem m2 = new MenuItem("Save");
          MenuItem m3 = new MenuItem("Exit");
          MenuItem m4 = new MenuItem("Cut");
          MenuItem m5 = new MenuItem("Copy");
          MenuItem m6 = new MenuItem("Paste");
    
          newmenu.getItems().add(m1);
          newmenu.getItems().add(m2);
          newmenu.getItems().add(m3);
          newmenu2.getItems().add(m4);
          newmenu2.getItems().add(m5);
          newmenu2.getItems().add(m6);
    
          MenuBar newmb = new MenuBar();
    
          newmb.getMenus().add(newmenu);
          newmb.getMenus().add(newmenu2);
          
          
          
          VBox box = new VBox(newmb);
          
           Scene scene = new Scene(box,400,200);  
           primaryStage.setScene(scene);  
           primaryStage.setTitle("JavaFX Menu Example");  
           primaryStage.show();  
        }
    public static void main(String[] args) {
        Application.launch(args);
    }
}

Output:

In order to create the Menu in JavaFX, we have to import all the required libraries such as javafx.application.Application, javafx.stage.Stage, javafx.scene.Scene, javafx.scene.control.Menu, javafx.scene.control.MenuBar, javafx.scene.control.MenuItem, javafx.scene.layout.VBox.

Then we have created one class named MenuUI extending the Application class. Also, we have to override the start method to provide implementation details. This method creates an object of Stage as primaryStage. For the container to hold a menu with menu items on the menu bar, a VBox object is created which is then passed to the Scene class object.

Two main menus are created namely File and Edit with menu items like Open, Save, Exit, and Cut, Copy, Paste respectively. Then menus are added to the menu bar.

The stage is prepared, the title is set and the show() method is called to display output. In order to run the application, the launch(args) method is called in the main() method. In output Frame like container is displayed with the title, "JavaFX Menu Example”. Also, it displays two menus File and Edit with menu items like Open, Save, Exit, and Cut, Copy, Paste respectively.

JavaFX Menu JavaFX Menu

JavaFX Action Handling on Menu:

Example:

import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.stage.Stage;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.control.Menu;
import javafx.scene.control.MenuBar;
import javafx.scene.control.MenuItem;
import javafx.scene.layout.VBox;


  
public class SliderUI extends Application {  


    @Override  
    public void start(Stage primaryStage) throws Exception 
    {  
      Menu newmenu = new Menu("File");
      Menu newmenu2 = new Menu("Edit");


          MenuItem m1 = new MenuItem("Open");
          MenuItem m2 = new MenuItem("Save");
          MenuItem m3 = new MenuItem("Exit");
          MenuItem m4 = new MenuItem("Cut");
          MenuItem m5 = new MenuItem("Copy");
          MenuItem m6 = new MenuItem("Paste");
    
          newmenu.getItems().add(m1);
          newmenu.getItems().add(m2);
          newmenu.getItems().add(m3);
          newmenu2.getItems().add(m4);
          newmenu2.getItems().add(m5);
          newmenu2.getItems().add(m6);
    
          MenuBar newmb = new MenuBar();
    
          newmb.getMenus().add(newmenu);
          newmb.getMenus().add(newmenu2);
          
          
          


          Label l = new Label("\t\t\t\t\t\t"
                  + "You have selected no menu items");
          EventHandler<ActionEvent> event = new EventHandler<ActionEvent>() {
  public void handle(ActionEvent e)
  {
      l.setText("\t\t\t\t\t\t" + ((MenuItem)e.getSource()).getText() + 
                                                     " menu item selected");
  }
};


m1.setOnAction(event);
m2.setOnAction(event);
m3.setOnAction(event);
m4.setOnAction(event);
m5.setOnAction(event);
m6.setOnAction(event);
    
          VBox box = new VBox(newmb,l);
          
           Scene scene = new Scene(box,400,200);  
           primaryStage.setScene(scene);  
           primaryStage.setTitle("JavaFX Menu Example");  
           primaryStage.show();  
        }
    public static void main(String[] args) {
        Application.launch(args);
    }
}

Output:

In order to create the Menu and action on it in JavaFX, we have to import all the required libraries such as javafx.application.Application, javafx.stage.Stage, javafx.scene.Scene, javafx.scene.control.Menu, javafx.scene.control.MenuBar, javafx.scene.control.MenuItem, javafx.scene.layout.VBox, javafx.event.ActionEvent, javafx.event.EventHandler;

Then we have created one class named MenuUI extending the Application class. Also, we have to override the start method to provide implementation details. This method creates an object of Stage as primaryStage. For the container to hold a menu with menu items on the menu bar with an action specified on it, a VBox object is created which is then passed to the Scene class object.

Two main menus are created namely File and Edit with menu items like Open, Save, Exit, and Cut, Copy, Paste respectively. Then menus are added to the menu bar. When we click on any menu item, it will show the message that the menu item is selected.

The stage is prepared, the title is set and the show() method is called to display output. In order to run the application, the launch(args) method is called in the main() method. In output Frame like container is displayed with the title, "JavaFX Menu Example". Also, it displays two menus File and Edit with menu items like Open, Save, Exit, and Cut, Copy, Paste respectively. When we click on the Save menu item, it will display the Save menu item selected. Similarly, When the user clicked on the Copy menu item, it displayed the Copy menu item selected.

JavaFX Menu JavaFX Menu JavaFX Menu

Related Topics

JavaFX Light Spot

In the JavaFX application, in order to apply various Light Spot effect from a single source, we use this class. It has the ability to apply light from all the...

4 minutes read.

JavaFX Line Chart

In the JavaFX application, in order to create a Line Chart, LineChart class is used. The LineChart allows us to plot markers point according to the entities on number axes....

4 minutes read.

JavaFX Bubble Chart

In the JavaFX application, in order to create Bubble Chart, the BubbleChart class is used. The Bubble Chart allows us to plot three dimensional data. All methods needed for this...

4 minutes read.

JavaFX Scatter Chart

In the JavaFX application, in order to create Scatter Chart, the ScatterChart class is used. The Scatter Chart allows us to plot data along the x and y axis of...

4 minutes read.

JavaFX Reflection

In the JavaFX application, in order to apply various reflection effect, we use this class. It has the ability to create the reflection of the given object. This effect adds...

4 minutes read.

JavaFX Motion Blur

In the JavaFX application, in order to apply various Motion blur effect, we use Motion Blur class. It has the ability to apply the Motion blur effect on the given...

4 minutes read.

JavaFX Shear

In the JavaFX application, in order to apply various shear effect, we use this class. It has the ability to shear the given object along the x or y axis....

3 minutes read.

JavaFX Label

The UI Control Label in a JavaFX is used to display the simple text. To use Label in JavaFX application javafx.scene.control.The label class is used. We can place Labels on...

3 minutes read.

JavaFX Scale Transition

In the JavaFX application, in order to generate the scale transition, we have to apply scale transition on any particular shape. It has the ability to scale the shape along...

4 minutes read.

JavaFX Gradient Color

In the JavaFX application, in order to draw any shape, we are using classes present in the javafx.scene.shape package. We can fill the gradient colors in these shapes using the...

7 minutes read.

JavaFX Shadow

In the JavaFX application, in order to apply various Shadow effect from a single source, we use this class. It has the ability to create the shadow of the given...

3 minutes read.

JavaFX Sequential Transition

In the JavaFX application, in order to generate the sequential transition, we have to apply sequential transition on any particular shape. It has the ability to execute multiple transactions sequentially...

4 minutes read.

JavaFX BorderPane

In the JavaFX application, in order to set Border Pane as a layout, the BorderPane class is used. The BorderPane layout allows us to arrange the components in the left,...

4 minutes read.

JavaFX Fill Transition

In the JavaFX application, in order to generate the fill transition, we have to apply fill transition on any particular shape. It has the ability to fill the given shape...

4 minutes read.

JavaFX Blend

In the JavaFX application, in order to apply various blend effect, we use Blend class. It has the ability to blend the colors of the shape. All methods needed for...

4 minutes read.

JavaFX Sphere

In the JavaFX application, in order to draw a Sphere, the Sphere class is used. The Sphere allows us to form a 3D shape having circular faces. All methods needed...

4 minutes read.

JavaFX CheckBox

In the JavaFX application, to get the user information in the form of choices from the user, a form is created using checkboxes that allow the user to select two...

4 minutes read.

JavaFX TextField

In the JavaFX application, to get the user information as input from the user, a form is created using textfields which allows the user to enter input for specific fields....

4 minutes read.

JavaFX Bloom

In the JavaFX application, in order to apply various bloom effect, we use the Bloom class. It has the ability to bloom the colors of the shape. All methods needed...

4 minutes read.

JavaFX Rectangle

In the JavaFX application, in order to draw a rectangle, the Rectangle class is used. The Rectangle allows us to join any four points with x and y coordinates in...

4 minutes read.