Updated March 28, 2023
Introduction to JavaFX MenuBar
MenuBar is just like a navigation bar with a list of items. Usually, we can see this MenuBar at the top of the screen which contains different menus. MenuBar class is available scene.control.MenuBar package. MenuBar again contains several menus. Menu contains different menu items. Menu class is available in javafx.scene.control.Menu package. We can also add sub menu items to the menu item.
Advantage:
- A structured way of data aligning.
- Easy to access the menu items.
Constructors of MenuBar:
- MenuBar(): Creates an instance non parameterized constructor with a new keyword.
- MenuBar(Menu m1,Menu m2, Menu m3……): Creates an instance set of Menus parameterized constructor with a new keyword.
Frequently Used Methods
Frequently used methods in menubar in javafx:
- hide(): hide() method hides the menu.
- show(): show() method shows the menu.
- getMenus(): The getMenus() method show all the menus inside the MenuBar.
- getItems(): getItems() method returns items of the menu.
- setUseSystemMenuBar(boolean b): setUseSystemMenuBar() method used to set the property to System Menu Bar.
- setOnShowing(EventHandler e): setOnShowing() method sets the property value showing.
- setOnShown(EventHandler e): setOnShown() method sets the property value shown.
- setOnHidden(EventHandler e): setOnHidden() method sets the property value hidden.
- setOnHiding(EventHandler e): setOnHiding() method sets the property value hiding.
How to Create MenuBar in JavaFX?
- Accessing JavaFX features user-defined class must extend Application.
- In JavaFX creating MenuBar is the first step. MenuBar can instantiate by using new.
MenuBar menuBar =new MenuBar();
- creating Menu is the second step. A menu can instantiate by using new.
Menu menu =new Menu();
- creating MenuItem is the third step. MenuItem can instantiate by using new.
MenuItem menuItem =new MenuItem();
- Adding MenuItem to the Menu is the 4th.
menu.getItems.add(menuItems);
- Adding MenuItem to the Menu is the 5th.
MenuBar.getMenus.add(menu);
- Create VBox or any other display class to add the menuBar is 6th.
VBox vBox=new VBox(menuBar); //Gives vertical box
- Creating a scene means screen to display output is the 7th.
Scene screen = new Scene(vBox, length, width);
- Adding Scene reference screen to the Stage object reference is 8th Adding output screen to Stage. We will get this stage object reference from the start predefined JavaFX method.
stage.setScene(screen);
- At last display output screen by showing stage reference with the show ().
stage.show();
Examples of MenuBar in JavaFX
Following are the example of the menubar in javafx:
1. Adding Menu Items to the MenuBar
Code:
package com.menubar;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Menu;
import javafx.scene.control.MenuBar;
import javafx.scene.control.MenuItem;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
public class AddingMenuItemsToMenuBar extends Application {
@Override
public void start(Stage primaryStage) {
// setting the title of application
primaryStage.setTitle("Adding Items to Menu Bar");
//creating menu bar
MenuBar menuBar=new MenuBar();
//creating menu for adding menu items
Menu menu=new Menu("Show");
//creating menu items
MenuItem companyName=new MenuItem("EDUCBA");
MenuItem courses=new MenuItem("Courses");
MenuItem aboutEDUCBA=new MenuItem("About EDUCBA");
MenuItem feeStructure=new MenuItem("Fee Structure");
//adding menu items to the menu
menu.getItems().add(companyName);
menu.getItems().add(courses);
menu.getItems().add(aboutEDUCBA);
menu.getItems().add(feeStructure);
//adding menu to the menu bar
menuBar.getMenus().add(menu);
//creating VBox for adding all menu bar
VBox vBox=new VBox(menuBar);
//adding scroll pane to the scene
Scene scene = new Scene(vBox, 401, 201);
primaryStage.setScene(scene);
//showing the output
primaryStage.show();
}
public static void main(String[] args) {
//invoking main method from JVM
launch(args);
}
}
Output:
Explanation:
- As you can see in the above output, we have added the first menu items to the menu and menu to the menu bar respectively.
- Added MenuBar to the vBox and displayed the output.
2. Adding Menu Items and Sub Menu Items to the MenuBar
Code:
package com.menubar;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Menu;
import javafx.scene.control.MenuBar;
import javafx.scene.control.MenuItem;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
public class AddingSubMenuItemsToMenuBar extends Application {
@Override
public void start(Stage primaryStage) {
// setting the title of application
primaryStage.setTitle("Adding Items to Menu Bar");
//creating menu bar
MenuBar menuBar=new MenuBar();
//creating menu for adding menu items
Menu menu=new Menu("Show");
//creating menu items
MenuItem companyName=new MenuItem("EDUCBA");
MenuItem aboutEDUCBA=new MenuItem("About EDUCBA");
MenuItem feeStructure=new MenuItem("Fee Structure");
//sub menu items for the courses
MenuItem javCourse=new MenuItem("Java");
MenuItem pythonCourse=new MenuItem("Python");
MenuItem cCourse=new MenuItem("C");
MenuItem angularCourse=new MenuItem("Angular JS");
//adding menu items to the menu
menu.getItems().add(companyName);
menu.getItems().add(aboutEDUCBA);
menu.getItems().add(feeStructure);
//adding sub menu items to the course menu
Menu subMenuCourse=new Menu("All Courses");
subMenuCourse.getItems().add(javCourse);
subMenuCourse.getItems().add(pythonCourse);
subMenuCourse.getItems().add(cCourse);
subMenuCourse.getItems().add(angularCourse);
menu.getItems().add(subMenuCourse);
//adding menu to the menu bar
menuBar.getMenus().add(menu);
//creating VBox for adding all menu bar
VBox vBox=new VBox(menuBar);
//adding scroll pane to the scene
Scene scene = new Scene(vBox, 401, 201);
primaryStage.setScene(scene);
//showing the output
primaryStage.show();
}
public static void main(String[] args) {
//invoking main method from JVM
launch(args);
}
}
Output:
Explanation:
- As you can see in the above output, we have added the first menu items to the menu, sub menu items to the submenu, sub menu items to the menu and menu to the menu bar respectively.
- Added MenuBar to the vBox and displayed the output.
3. Adding Menu Items to the MenuBar
Code:
package com.menubar;
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
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;
import javafx.stage.Stage;
public class AddingEventActionToMenuItems extends Application {
@Override
public void start(Stage primaryStage) {
// setting the title of application
primaryStage.setTitle("Adding Items to Menu Bar");
//creating menu bar
MenuBar menuBar=new MenuBar();
//creating menu for adding menu items
Menu menu=new Menu("Show");
//creating menu items
MenuItem companyName=new MenuItem("EDUCBA");
MenuItem courses=new MenuItem("Courses");
MenuItem aboutEDUCBA=new MenuItem("About EDUCBA");
MenuItem feeStructure=new MenuItem("Fee Structure");
//adding menu items to the menu
menu.getItems().add(companyName);
menu.getItems().add(courses);
menu.getItems().add(aboutEDUCBA);
menu.getItems().add(feeStructure);
//adding menu to the menu bar
menuBar.getMenus().add(menu);
//creating label for adding menu item selection
Label selectionLabel=new Label();
EventHandler<ActionEvent> event = new EventHandler<ActionEvent>() {
public void handle(ActionEvent e)
{
selectionLabel.setText("\t\t\t\t You have " +((MenuItem)e.getSource()).getText() +
" item selected");
}
};
//adding event to the menu items
companyName.setOnAction(event);
courses.setOnAction(event);
aboutEDUCBA.setOnAction(event);
feeStructure.setOnAction(event);
//creating VBox for adding all menu bar
VBox vBox=new VBox(menuBar, selectionLabel);
//adding scroll pane to the scene
Scene scene = new Scene(vBox, 401, 201);
primaryStage.setScene(scene);
//showing the output
primaryStage.show();
}
public static void main(String[] args) {
//invoking main method from JVM
launch(args);
}
}
Output:
Explanation:
- As you can see in the above output, we have added the first menu items to the menu and menu to the menu bar respectively.
- Added MenuBar and label to the vBox and displayed the output.
Recommended Articles
This is a guide to JavaFX MenuBar. Here we discuss the Introduction and how to create menubar in javafx along with different examples and its code implementation. You may also have a look at the following articles to learn more –