You are reading the article How To Create Menubar In Javafx With Syntax updated in October 2023 on the website Saigonspaclinic.com. We hope that the information we have shared is helpful to you. If you find the content interesting and meaningful, please share it with your friends and continue to follow and support us for the latest updates. Suggested November 2023 How To Create Menubar In Javafx With Syntax
Introduction to JavaFX MenuBarWeb development, programming languages, Software testing & others
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 MethodsFrequently 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);
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 JavaFXFollowing are the example of the menubar in javafx:
1. Adding Menu Items to the MenuBarCode:
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) { primaryStage.setTitle("Adding Items to Menu Bar"); MenuBar menuBar=new MenuBar(); Menu menu=new Menu("Show"); MenuItem companyName=new MenuItem("EDUCBA"); MenuItem courses=new MenuItem("Courses"); MenuItem aboutEDUCBA=new MenuItem("About EDUCBA"); MenuItem feeStructure=new MenuItem("Fee Structure"); menu.getItems().add(companyName); menu.getItems().add(courses); menu.getItems().add(aboutEDUCBA); menu.getItems().add(feeStructure); menuBar.getMenus().add(menu); VBox vBox=new VBox(menuBar); Scene scene = new Scene(vBox, 401, 201); primaryStage.setScene(scene); primaryStage.show(); } public static void main(String[] args) { 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 MenuBarCode:
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) { primaryStage.setTitle("Adding Items to Menu Bar"); MenuBar menuBar=new MenuBar(); Menu menu=new Menu("Show"); MenuItem companyName=new MenuItem("EDUCBA"); MenuItem aboutEDUCBA=new MenuItem("About EDUCBA"); MenuItem feeStructure=new MenuItem("Fee Structure"); MenuItem javCourse=new MenuItem("Java"); MenuItem pythonCourse=new MenuItem("Python"); MenuItem cCourse=new MenuItem("C"); MenuItem angularCourse=new MenuItem("Angular JS"); menu.getItems().add(companyName); menu.getItems().add(aboutEDUCBA); menu.getItems().add(feeStructure); 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); menuBar.getMenus().add(menu); VBox vBox=new VBox(menuBar); Scene scene = new Scene(vBox, 401, 201); primaryStage.setScene(scene); primaryStage.show(); } public static void main(String[] args) { launch(args); } }Output:
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 MenuBarCode:
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) { primaryStage.setTitle("Adding Items to Menu Bar"); MenuBar menuBar=new MenuBar(); Menu menu=new Menu("Show"); MenuItem companyName=new MenuItem("EDUCBA"); MenuItem courses=new MenuItem("Courses"); MenuItem aboutEDUCBA=new MenuItem("About EDUCBA"); MenuItem feeStructure=new MenuItem("Fee Structure"); menu.getItems().add(companyName); menu.getItems().add(courses); menu.getItems().add(aboutEDUCBA); menu.getItems().add(feeStructure); menuBar.getMenus().add(menu); Label selectionLabel=new Label(); public void handle(ActionEvent e) { selectionLabel.setText("tttt You have " +((MenuItem)e.getSource()).getText() + " item selected"); } }; companyName.setOnAction(event); courses.setOnAction(event); aboutEDUCBA.setOnAction(event); feeStructure.setOnAction(event); VBox vBox=new VBox(menuBar, selectionLabel); Scene scene = new Scene(vBox, 401, 201); primaryStage.setScene(scene); primaryStage.show(); } public static void main(String[] args) { 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 ArticlesThis 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 –
You're reading How To Create Menubar In Javafx With Syntax
Update the detailed information about How To Create Menubar In Javafx With Syntax on the Saigonspaclinic.com website. We hope the article's content will meet your needs, and we will regularly update the information to provide you with the fastest and most accurate information. Have a great day!