JavaFX – MenuBar


JavaFX – MenuBar



”;


A menubar is a graphical user interface component that displays a horizontal row of menus, each containing a list of commands or options. It is located at the top of a window or screen, and provides a convenient way for users to access the functionality of an application. A typical menubar looks like the below figure −


Sample Menubar

Creating MenuBar in JavaFX

In JavaFX, the menubar control is represented by a class named MenuBar. This class belongs to the package javafx.scene.control. By instantiating this class, we can create a MenuBar control in JavaFX.

In addition to the MenuBar class, we also require the following classes −

Menu

The Menu class represents a single menu in the menubar. It has a text property that defines its label, and an items property that holds a list of menu items. To create a Menu, use the code given below −


//Creating a menu
Menu objectOfMenu = new Menu("nameOfMenu");

MenuItem

The MenuItem is used to create a single command or option within a menu. It has a text property that defines its label, and an onAction property that defines what happens when the user selects it. It is created by using the below code −


//Creating menu items for the menu
MenuItem menuItemObject = new MenuItem("nameOfMenuItem");

Example

The following example illustrates how to create a MenuBar in JavaFX application. Save this code in a file with the name JavafxmenuBar.java.


import javafx.application.Application;
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.stage.Stage;
public class JavafxmenuBar extends Application {
   public void start(Stage stage) {
      //Creating the first menu
      Menu category = new Menu("Category");
      //Creating menu items for the menu
      MenuItem itemOne = new MenuItem("Programming");
      MenuItem itemTwo = new MenuItem("Cyber Security");
      MenuItem itemThree = new MenuItem("Marketing");
      //Adding all the items to the category
      category.getItems().addAll(itemOne, itemTwo, itemThree);
      //Creating another menu
      Menu library = new Menu("Library");
      //Creating menu items for the library menu
      MenuItem itemFour = new MenuItem("HTML");
      MenuItem itemFive = new MenuItem("Java");
      MenuItem itemSix = new MenuItem("JavaFX");
      //Adding all the items to library menu
      library.getItems().addAll(itemFour, itemFive, itemSix);
      //Creating the third menu
      Menu articles = new Menu("Articles");
      //Creating menu items for the articles
      MenuItem itemSeven = new MenuItem("Constructor");
      MenuItem itemEight = new MenuItem("Inheritance");
      MenuItem itemNine = new MenuItem("Current Affairs");
      //Adding all items to the menu
      articles.getItems().addAll(itemSeven, itemEight, itemNine);
      //Instantiating the MenuBar class
      MenuBar newmenubar = new MenuBar();
      //Adding all the menus to the MenuBar object
      newmenubar.getMenus().addAll(category, library, articles);
      //Setting the stage
      Group newgroup = new Group(newmenubar);
      Scene scene = new Scene(newgroup, 600, 400);
      stage.setTitle("MenuBar in JavaFX");
      stage.setScene(scene);
      stage.show();
   }
   public static void main(String args[]){
      launch(args);
   }
}

To compile and execute the saved Java file from the command prompt, use the following commands −


javac --module-path %PATH_TO_FX% --add-modules javafx.controls JavafxmenuBar.java
java --module-path %PATH_TO_FX% --add-modules javafx.controls JavafxmenuBar

Output

When we execute the above code, it will generate the following output.


MenuBar Output

How to add Icons to the MenuItems?

To add icons to the menuitems, we call the setGraphic() method which accepts an object of ImageView class. The icon will be appear next to the MenuItem name.

Example

In the following JavaFX application, we are going to demonstrate how to add icons for the menuitems within MenuBar. Save this code in a file with the name JavafxmenuBar.java.


import javafx.application.Application;
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.stage.Stage;
import java.io.File;
import javafx.scene.image.ImageView;
public class JavafxmenuBar extends Application {
   public void start(Stage stage) {
      //Creating the first menu
      Menu category = new Menu("Category");
      //Creating menu items for the menu
      MenuItem itemOne = new MenuItem("Programming");
      MenuItem itemTwo = new MenuItem("Cyber Security");
      MenuItem itemThree = new MenuItem("Marketing");
      // adding icons to the menuitems
      itemOne.setGraphic(new ImageView("file:programming.png"));
      itemTwo.setGraphic(new ImageView("file:cyber.png"));
      itemThree.setGraphic(new ImageView("file:marketing.png"));
      //Adding all the items to the category
      category.getItems().addAll(itemOne, itemTwo, itemThree);
      //Creating another menu
      Menu library = new Menu("Library");
      //Creating menu items for the library menu
      MenuItem itemFour = new MenuItem("HTML");
      MenuItem itemFive = new MenuItem("Java");
      MenuItem itemSix = new MenuItem("JavaFX");
      //Adding all the items to library menu
      library.getItems().addAll(itemFour, itemFive, itemSix);
      //Creating the third menu
      Menu articles = new Menu("Articles");
      //Creating menu items for the articles
      MenuItem itemSeven = new MenuItem("Constructor");
      MenuItem itemEight = new MenuItem("Inheritance");
      MenuItem itemNine = new MenuItem("Current Affairs");
      //Adding all items to the menu
      articles.getItems().addAll(itemSeven, itemEight, itemNine);
      //Instantiating the MenuBar class
      MenuBar newmenubar = new MenuBar();
      //Adding all the menus to the MenuBar object
      newmenubar.getMenus().addAll(category, library, articles);
      //Setting the stage
      Group newgroup = new Group(newmenubar);
      Scene scene = new Scene(newgroup, 600, 400);
      stage.setTitle("MenuBar in JavaFX");
      stage.setScene(scene);
      stage.show();
   }
   public static void main(String args[]){
      launch(args);
   }
}

Compile and execute the saved Java file from the command prompt by using the following commands −


javac --module-path %PATH_TO_FX% --add-modules javafx.controls JavafxmenuBar.java
java --module-path %PATH_TO_FX% --add-modules javafx.controls JavafxmenuBar

Output

On executing the above JavaFX code, it will generate the following output.


MenuBar Output2

Advertisements

”;

Leave a Reply

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