Kensoft PH
  • Download
    • KenshotApplication
  • Contact
  • About
Java Quiz
No Result
View All Result
Kensoft PH
  • Download
    • KenshotApplication
  • Contact
  • About
Java Quiz
No Result
View All Result
Kensoft PH
No Result
View All Result
Home Java

How to use the Menu Bar in JavaFX | 100% Perfect Tutorial

December 3, 2022 - Updated on January 23, 2023
in Java
Reading Time: 7 mins read
0
JavaFX MenuBar
430
VIEWS
Share on FacebookShare on TwitterShare via Email

JavaFX MenuBar is one of the essentials when developing a desktop application. In this tutorial, we will make the Menu bar, and you will learn how to use the Menu bar in JavaFX. The menu bar is the same navigation bar on the website and creating a Menu bar in a desktop application is very easy, but it still depends on the programming language. In this case, we will make the Menu bar, and you will learn how to use the Menu bar in JavaFX.

A menu bar is a horizontal bar that holds the menus in your application. In this tutorial, I will walk you through creating the menubar in your JavaFX application and show more examples for you to learn more about the Menu bar in JavaFX. Keep reading below to learn more, the first step we are going to do is create the menu bar and show it to the scene graph and to be followed by other examples.

Contents

Toggle
  • How to use the Menu Bar in JavaFX
    • Add the JavaFX Menubar to the scene graph
      • Output
    • Use SystemMenuBar property
    • Adding graphics to the Menus
    • Adding JavaFX Menu Item
    • Add the Menu item to Menu
      • Output
    • Adding graphic to the Menu Item
    • Adding an event to a menu item
      • Output
    • Adding JavaFX RadioMenuItem
    • Grouping using ToggleGroup
    • Add the RadioMenuItem to the Menu
    • Adding an event to our RadioMenuItem
    • Working with Submenu items in JavaFX
      • Output
    • Working with CustomMenuItem
      • Output
    • Working with SeparatorMenuItem in JavaFX
      • Output
  • YouTube Video

How to use the Menu Bar in JavaFX

Creating the Menu bar is very easy and there are two ways for you to choose to create it easily, you either use the Scene Builder application to create the graphical user interface in JavaFX easily or code it manually, and I prefer to show coding examples here rather than using the Scene Builder because I want you to learn on how to code it.

To create this control, we will use its default constructor by calling the MenuBar class and instantiating it. Please go ahead to the example code below to see how it is created.

// creating the menu bar using its default constructor
MenuBar menuBar = new MenuBar();

Add the JavaFX Menubar to the scene graph

To add our Menu bar to the scene graph, we also need to create the layout in JavaFX as a container for our nodes. In this example, I’m going to use the BorderPane layout. Creating and adding it to the scene is very easy to do, proceed to the example code below to learn more.

// create the layout
BorderPane layout = new BorderPane();
// create the menubar
MenuBar menuBar = new MenuBar();
// adding menus
Menu file = new Menu("File");
Menu about = new Menu("About");
// set to top in borderPane layout
layout.setTop(menuBar);
menuBar.getMenus().addAll(file,about);
// create the scene
Scene scene = new Scene(layout, 500, 500);
stage.setScene(scene);
stage.setTitle("JavaFX MenuBar");
stage.show();

Output

How to use the menu bar in javafx

Use SystemMenuBar property

This property is a boolean type, and you can set it to true or false. It will use the system menu bar if the platform supports it. If you are using Mac and your Mac supports a system menu bar then the MenuBar will use the system menu bar to display its items.

menuBar.setUseSystemMenuBar(true);

Adding graphics to the Menus

You have already created the menus above. The menu is the buttons in the menu bar. You can also add a graphic to the menu by using the setGraphic() method. Please go ahead below to learn more.

Menu file = new Menu("File");
file.setGraphic(new ImageView(new Image("file.png")));

Adding JavaFX Menu Item

JavaFX Menu Item is part of the Menu Bar in JavaFX. The Menu Item is the button that will pop up if you click the menus of the menu bar. To create the Menu Items, you need to use its constructor. Please see the following example code below to learn more about it.

MenuItem item1 = new MenuItem("Open");
MenuItem item2 = new MenuItem("Save");
MenuItem item3 = new MenuItem("Exit");

Add the Menu item to Menu

You need to use the Menu object and use the getItems() method to add the menu items to a specific menu button. In this tutorial, we will use the File menu, and let’s add the JavaFX Menu Item. Please proceed below to learn more.

// call the file menu (refer to the example code above)
file.getItems().add(item1, item2, item3);

Output

Menu Bar in JavaFX with JavaFX Menu Item

Adding graphic to the Menu Item

Adding a graphic to the menu item is the same as how you add a graphic to the Menus. Please continue below to learn more.

MenuItem item1 = new MenuItem("Open");
item1.setGraphic(new ImageView(new Image("open.png")));

Adding an event to a menu item

Setting an event to a menu item is very important to make your menu items work to perform an action when the user wants to click the JavaFX Menu Item. In this example, we will set an action event for our menu items.

item1.setOnAction(event -> {
     alert();
});
item2.setOnAction(event -> {
     alert();
});
item3.setOnAction(event -> {
     alert();
});

private void alert(){
    Alert alert = new Alert(Alert.AlertType.INFORMATION);
    alert.setTitle("JavaFX MenuBar");
    alert.setHeaderText("Menu Item");
    alert.setContentText("Clicked");
    alert.show();
}

The method alert() shows an alert when one of the menu items is clicked. Creating an alert in JavaFX is also easy, and you can also implement the alert in your application.

Output

JavaFX Menu Item clicked

Adding JavaFX RadioMenuItem

JavaFX RadioMenuItem is also a menu item, but it looks different because it has a checkmark on the left side of the item when you select it. When you want to add multiple RadioMenuItem, you will also need to use the ToggleGroup if you only want one RadioMenuItem to be selected, just like how you use it on ToggleButton, RadioButton, or CheckBox. We need to use the RadioMenuItem’s constructor to create this item, and it is really easy to do.

// Create the RadioMenuItem
RadioMenuItem r1 = new RadioMenuItem("True");
RadioMenuItem r2 = new RadioMenuItem("False");

Grouping using ToggleGroup

Grouping the RadioMenuItem using the ToggleGroup allows you to only select one RadioMenuItem. The following example code below will show you how to add them to the ToggleGroup.

RadioMenuItem r1 = new RadioMenuItem("True");
RadioMenuItem r2 = new RadioMenuItem("False");
ToggleGroup rGroup = new ToggleGroup();
rGroup.getToggles().addAll(r1, r2);

Add the RadioMenuItem to the Menu

To make our RadioMenuItem visible to our application, you also need to add them to the Menu. As mentioned above, you need to use the getItems() method to add items to the Menu.

Menu menu = new Menu("Save As");
menu.getItems().addAll(r1, r2);

Adding an event to our RadioMenuItem

Adding an event to our RadioMenuItem is very easy, just like adding an event to other nodes. This will make our nodes work or function. The example code below will show you how to add an event to the JavaFX RadioMenuItem.

RadioMenuItem r1 = new RadioMenuItem("True");
RadioMenuItem r2 = new RadioMenuItem("False");
ToggleGroup rGroup = new ToggleGroup();
rGroup.getToggles().addAll(r1, r2);

r1.setOnAction(event -> {
    alert();
});
r2.setOnAction(event -> {
    alert();
});

Working with Submenu items in JavaFX

Submenu is also important if you have a bunch of options in that particular menu. Creating a Submenu in JavaFX is very easy and what you need to do is to create the main menu. For example, a “save as” submenu also uses the Menu class to create the submenu but to make it a submenu, you need to add this menu to the main menu and then create menu items for it. The example code below will show you how it is done.

MenuBar menuBar = new MenuBar();
Menu file = new Menu("File");
Menu about = new Menu("About");
menuBar.getMenus().addAll(file,about);

// this RadioMenuItem
RadioMenuItem r1 = new RadioMenuItem("True");
RadioMenuItem r2 = new RadioMenuItem("False");
ToggleGroup rGroup = new ToggleGroup();
rGroup.getToggles().addAll(r1, r2);

// This one submenu item
Menu submenu = new Menu("Save As");
submenu.getItems().addAll(r1, r2);

file.getItems().addAll(item1, item2, submenu, item3);

Output

JavaFX Submenu

Working with CustomMenuItem

A CustomMenuItem is one of the powerful menu item types. It is powerful because it can handle nodes inside the CustomMenuItem. For example, you can add a Button, Textfield, or an HBox as a MenuItem. The example code below will show you how to create the CustomMenuItem in JavaFX.

// Custom Menu Item
CustomMenuItem customItem = new CustomMenuItem();
TextField text = new TextField();
customItem.setHideOnClick(false);
customItem.setContent(text);

If you noticed the setHideOnClick() method is used to stay visible when you are interacting with the TextField inside the CustomMenuItem. Otherwise, it will hide immediately if you are clicking the TextField when the setHideOnClick() method is true. Try it yourself to check it out.

Output

JavaFX CustomMenuItem

Working with SeparatorMenuItem in JavaFX

SeparatorMenuItem is just a separator, it is used to separate menu items if the item belongs to the other group. The following example code below will show you how to add a SeparatorMenuItem to JavaFX.

MenuBar menuBar = new MenuBar();
Menu file = new Menu("File");
Menu about = new Menu("About");
menuBar.getMenus().addAll(file,about);

// this RadioMenuItem
RadioMenuItem r1 = new RadioMenuItem("True");
RadioMenuItem r2 = new RadioMenuItem("False");
ToggleGroup rGroup = new ToggleGroup();
rGroup.getToggles().addAll(r1, r2);

// This one submenu item
Menu submenu = new Menu("Save As");
submenu.getItems().addAll(r1, r2);

SeparatorMenuItem separatorMenuItem = new SeparatorMenuItem();

file.getItems().addAll(item1, item2, submenu, separatorMenuItem, item3);

Output

JavaFX SeparatorMenuItem

YouTube Video

YouTube video
Previous Post

How to use the Slider in JavaFX | 100% Perfect Tutorial

Next Post

How to use the JavaFX ContextMenu | 100% Perfect Tutorial

KENSOFT

KENSOFT

What’s up! Kent is my name. The name KENSOFT is derived from the words Kent and Software. My programming language of choice is Java

Related tutorials

How to Use the JavaFX Pie Chart 100% For Beginners
Java

How to Use the JavaFX Pie Chart 100% For Beginners

June 12, 2024 - Updated on October 6, 2024
205
How to Connect to an API Using JavaFX
Java

How to Connect to an API Using JavaFX

May 26, 2024 - Updated on September 28, 2024
215
JavaFX SQLite Database CRUD Tutorial
Java

JavaFX SQLite Database CRUD Tutorial | Note Application

May 26, 2024 - Updated on September 28, 2024
586
Next Post
JavaFX Context Menu

How to use the JavaFX ContextMenu | 100% Perfect Tutorial

How to use the JavaFX ToolBar

How to use the JavaFX ToolBar | 100% Perfect Tutorial

JavaFX TabPane Tutorial

How to use the TabPane in JavaFX | 100% Perfect Tutorial

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Tools

Multi-platform installer builder

Java profiler

  • Trending
  • Comments
  • Latest
MySQL database using XAMPP

How to connect Java to MySQL database using Xampp server | 100% best for beginners

October 27, 2020 - Updated on January 23, 2023
Failed to automatically set up a JavaFX Platform

Failed to automatically set up a JavaFX Platform SOLVED Apache NetBeans 12.3 | Best way

April 11, 2021 - Updated on July 3, 2022
JavaFX 17

How To install JDK 17 and JavaFX 17 on NetBeans IDE | Best

November 15, 2021 - Updated on December 13, 2021
hide and show password in jPasswordField

JPasswordField in Java Hide or Show Password | 100% best for beginners

April 2, 2021 - Updated on September 21, 2022
Failed to automatically set up a JavaFX Platform

Failed to automatically set up a JavaFX Platform SOLVED Apache NetBeans 12.3 | Best way

3DES in Java and AES in Java

How to use AES and 3DES in Java | 100% best for beginners

JavaFX Splash Screen

How to create JavaFX Splash Screen | 100% best for beginners

set up JavaFX and Scene Builder

How to set up JavaFX and Scene Builder in NetBeans IDE | 100% best for beginners

How to Use the JavaFX Pie Chart 100% For Beginners

How to Use the JavaFX Pie Chart 100% For Beginners

June 12, 2024 - Updated on October 6, 2024
How to Connect to an API Using JavaFX

How to Connect to an API Using JavaFX

May 26, 2024 - Updated on September 28, 2024
JavaFX SQLite Database CRUD Tutorial

JavaFX SQLite Database CRUD Tutorial | Note Application

May 26, 2024 - Updated on September 28, 2024
How to take a screenshot on PC using Kenshot

How to Take a Screenshot on PC Using Kenshot: A Full Guide

January 18, 2024 - Updated on October 6, 2024

Latest Tutorials

How to Use the JavaFX Pie Chart 100% For Beginners

How to Use the JavaFX Pie Chart 100% For Beginners

June 12, 2024 - Updated on October 6, 2024
How to Connect to an API Using JavaFX

How to Connect to an API Using JavaFX

May 26, 2024 - Updated on September 28, 2024
JavaFX SQLite Database CRUD Tutorial

JavaFX SQLite Database CRUD Tutorial | Note Application

May 26, 2024 - Updated on September 28, 2024

Popular Tutorials

  • MySQL database using XAMPP

    How to connect Java to MySQL database using Xampp server | 100% best for beginners

    0 shares
    Share 0 Tweet 0
  • Failed to automatically set up a JavaFX Platform SOLVED Apache NetBeans 12.3 | Best way

    0 shares
    Share 0 Tweet 0
  • How To install JDK 17 and JavaFX 17 on NetBeans IDE | Best

    0 shares
    Share 0 Tweet 0
Facebook Instagram Youtube Github LinkedIn Discord
Kensoft PH

What’s up! I'm Kent. The name KENSOFT is derived from the words Kent and Software. My programming language of choice is Java, which I use to create computer applications. In a company, I created applications and a website.

Categories

Website

Check the status

Privacy Policy

Terms and Condition

Sitemap

Latest Tutorials

How to Use the JavaFX Pie Chart 100% For Beginners

How to Use the JavaFX Pie Chart 100% For Beginners

June 12, 2024 - Updated on October 6, 2024
How to Connect to an API Using JavaFX

How to Connect to an API Using JavaFX

May 26, 2024 - Updated on September 28, 2024
JavaFX SQLite Database CRUD Tutorial

JavaFX SQLite Database CRUD Tutorial | Note Application

May 26, 2024 - Updated on September 28, 2024

© 2024 Made With Love By KENSOFT PH

No Result
View All Result
  • Download
    • Kenshot
  • Contact
  • About
  • Java Quiz

© 2024 Made With Love By KENSOFT PH

This website uses cookies. By continuing to use this website you are giving consent to cookies being used. Visit our Privacy and Cookie Policy.