JavaFX Menu and Nested Menus Tutorial
Introduction to Menu in JavaFX
A menu in JavaFX is a popup window that contains a list of items. Users can select an item from the menu to trigger an action. Menus are typically used to provide users with access to a variety of commands or options.
There are two types of menus in JavaFX:
- Menubar:A menubar is a horizontal bar that is typically located at the top of the window. It contains a list of menus.
- Context menu: A context menu is a popup menu that appears when the user right-clicks on a node in the scene.
Menus can contain a variety of items, including:
- Menu items: Menu items are the basic building blocks of menus. They allow users to trigger actions.
- Submenus: Submenus are menus that are contained within other menus. They allow users to group related menu items together.
- Separators: Separators are horizontal lines that can be used to separate menu items.
To create a menu in JavaFX, you need to create a Menu object. You can then add menu items to the menu using the `getItems()` method. To add a submenu to a menu, you need to create a Menu object and add it to the menu using the `getItems()` method.
Here is an example of how to create a simple menu in JavaFX:
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Menu;
import javafx.scene.control.MenuItem;
import javafx.stage.Stage;
public class MyApp extends Application {
@Override
public void start(Stage stage) throws Exception {
Menu fileMenu = new Menu("File");
MenuItem openMenuItem = new MenuItem("Open");
MenuItem saveMenuItem = new MenuItem("Save");
MenuItem exitMenuItem = new MenuItem("Exit");
fileMenu.getItems().addAll(openMenuItem, saveMenuItem, exitMenuItem);
MenuBar menuBar = new MenuBar();
menuBar.getMenus().add(fileMenu);
Scene scene = new Scene(menuBar);
stage.setTitle("My JavaFX Application");
stage.setScene(scene);
stage.show();
}
public static void main(String[] args) {
launch(args);
}
}
This application will create a window with a menubar that contains a single menu, the File menu. The File menu contains three menu items: Open, Save, and Exit.
To handle menu item events, you can use the `setOnAction()` method. The `setOnAction()` method takes a lambda expression as its argument. The lambda expression will be executed when the menu item is selected.
Here is an example of how to handle the click event for the Open menu item:
openMenuItem.setOnAction(event -> {
// Open a file
});
Menus are a powerful tool for providing users with access to a variety of commands or options. By understanding how to use menus, you can create JavaFX applications with user-friendly interfaces.
Exercises
Code for creating a menu bar in javafx
package practiceproject;
import java.io.*;
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.Event;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.Menu;
import javafx.scene.control.MenuBar;
import javafx.scene.layout.BorderPane;
import javafx.scene.paint.Color;
import javafx.scene.text.Font;
import javafx.stage.Stage;
public class democlass extends Application{
public static void main(String[] args){
launch(args);
}
@Override
public void start(Stage primaryStage) throws Exception {
//creating a menubar in JavaFX
MenuBar main_menu = new MenuBar();
Menu File = new Menu("File");
Menu Edit = new Menu("Edit");
Menu Source = new Menu("Source");
Menu Refactor = new Menu("Refactor");
main_menu.getMenus().add(File);
main_menu.getMenus().add(Edit);
main_menu.getMenus().add(Source);
main_menu.getMenus().add(Refactor);
BorderPane root = new BorderPane();
root.setTop(main_menu);
//add this layout to a scene
Scene sc = new Scene(root);
//set scene with primary stage
primaryStage.setTitle("Javafx UI Control Demo");
primaryStage.setScene(sc);
primaryStage.setWidth(500);
primaryStage.setHeight(500);
primaryStage.show();
}
}
Code for creating drop down items for menu bar in javafx
package practiceproject;
import java.io.*;
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.Event;
import javafx.event.EventHandler;
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.scene.paint.Color;
import javafx.scene.text.Font;
import javafx.stage.Stage;
public class democlass extends Application{
public static void main(String[] args){
launch(args);
}
@Override
public void start(Stage primaryStage) throws Exception {
//creating a menubar in JavaFX
MenuBar main_menu = new MenuBar();
Menu File = new Menu("File");
Menu Edit = new Menu("Edit");
Menu Source = new Menu("Source");
Menu Refactor = new Menu("Refactor");
main_menu.getMenus().add(File);
main_menu.getMenus().add(Edit);
main_menu.getMenus().add(Source);
main_menu.getMenus().add(Refactor);
//adding some menu items to file menu
MenuItem New = new MenuItem("New");
MenuItem Save = new MenuItem("Save");
MenuItem Close = new MenuItem("Close");
File.getItems().add(New);
File.getItems().add(Save);
File.getItems().add(Close);
BorderPane root = new BorderPane();
root.setTop(main_menu);
//add this layout to a scene
Scene sc = new Scene(root);
//set scene with primary stage
primaryStage.setTitle("Javafx UI Control Demo");
primaryStage.setScene(sc);
primaryStage.setWidth(500);
primaryStage.setHeight(500);
primaryStage.show();
}
}
Code for creating sub menus in javafx
package practiceproject;
import java.io.*;
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.Event;
import javafx.event.EventHandler;
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.scene.paint.Color;
import javafx.scene.text.Font;
import javafx.stage.Stage;
public class democlass extends Application{
public static void main(String[] args){
launch(args);
}
@Override
public void start(Stage primaryStage) throws Exception {
//creating a menubar in JavaFX
MenuBar main_menu = new MenuBar();
Menu File = new Menu("File");
Menu Edit = new Menu("Edit");
Menu Source = new Menu("Source");
Menu Refactor = new Menu("Refactor");
main_menu.getMenus().add(File);
main_menu.getMenus().add(Edit);
main_menu.getMenus().add(Source);
main_menu.getMenus().add(Refactor);
//adding some menu items to file menu
Menu New = new Menu("New");
Menu Save = new Menu("Save");
Menu Close = new Menu("Close");
File.getItems().add(New);
File.getItems().add(Save);
File.getItems().add(Close);
MenuItem JavaProject = new MenuItem("Java Project");
MenuItem Project = new MenuItem("Project");
New.getItems().add(JavaProject);
New.getItems().add(Project);
BorderPane root = new BorderPane();
root.setTop(main_menu);
//add this layout to a scene
Scene sc = new Scene(root);
//set scene with primary stage
primaryStage.setTitle("Javafx UI Control Demo");
primaryStage.setScene(sc);
primaryStage.setWidth(500);
primaryStage.setHeight(500);
primaryStage.show();
}
}