Object Oriented Programming: Unit V: JAVAFX Event Handling, Controls and Components

JAVAFX Menus

with Example Java Programs

In JavaFX we can create a menu using a Menu class.For using the Menu class in our JavaFX application we need to import javafx.scene.control.

Menus

Menu

• In JavaFX we can create a menu using a Menu class.

• For using the Menu class in our JavaFX application we need to import javafx.scene.control. Menu so that the methods associated with this class can be used in the application.

•  Menu is a popup menu that contains several menu items that are displayed when the user clicks a menu. The user can select a menu item from the menu.

• Constructors for Menu class are

• Commonly used Methods are

Menu Bars

• MenuBar is just like a navigation bar with menus on it. The menubar is located at the the screen.

• The MenuBar class is available javafx.scene.control.MenuBar package.   

• Constructors for MenuBar class are

Menultem

• A MenuItem is a basic item that goes on a menu.

• Following Fig. 5.6.1 shows the menu,menubar and menuitems.


Steps for creating JavaFX application for creating Menu

Step 1: Create a Menu bar is the first step, MenuBar can be instantiated using new

MenuBar menuBar = new MenuBar();

Step 2: Now create the menu. The name of the menu is passed as an argument to the Menu class.

Menu fileMenu = new Menu("File");

Step 3: Create the menuitem. The name of the menu is passed as an argument to the MenuItem class.

MenuItem newItem = new MenuItem("New");

Step 4: Add menu items to the menu using add or addAll method

fileMenu.getItems().addAll(newItem, openFileItem, saveltem, exitItem);

Step 5: Add Menu to Menu Bar using add or addAll method.

menuBar.getMenus().addAll(file Menu, editMenu, aboutMenu);

Programming Example

package application;

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.BorderPane;

import javafx.stage.Stage;

public class Main extends Application {

@Override

public void start (Stage stage) {

// Creating MenuBar

MenuBar menuBar = new MenuBar();

// Creating three menus

Menu file Menu = new Menu("File");

Menu editMenu = new Menu("Edit");

Menu aboutMenu = new Menu("About");

// Creating MenuItems for File menu

MenuItem newItem = new MenuItem("New");

MenuItem openFileItem = new MenuItem("Open File");

MenuItem saveItem = new MenuItem("Save");

MenuItem exitItem = new MenuItem("Exit");

// Creating MenuItems for Edit menu

MenuItem cutItem = new MenuItem("Cut");

MenuItem copyItem = new MenuItem("Copy");

MenuItem pasteItem = new MenuItem("Paste");

//There is no menu item for About menu

//Now Adding menuItems to the Menus

fileMenu.getItems().addAll(newItem, openFileItem, saveItem, exitItem);

 editMenu.getItems().addAll(cutItem,copyItem, pasteItem);

//Adding Menus to the MenuBar

menuBar.getMenus().addAll(fileMenu, editMenu, aboutMenu);

BorderPane root = new BorderPane();

root.setTop(menuBar);

Scene scene = new Scene(root, 400, 300);

stage.setTitle("JavaFX Menu Demo");

stage.setScene(scene);

stage.show();

}

public static void main(String[] args) {

Application.launch(args);

}

}

Output



Review Questions

1.Write and explain the constructors and commonly used methods for Menu. 2.Write a JavaFX application to display menu, menubar and menuitems.

Object Oriented Programming: Unit V: JAVAFX Event Handling, Controls and Components : Tag: : with Example Java Programs - JAVAFX Menus