Le Tutoriel de JavaFX Menu

View more Tutorials:

1- JavaFX Menu

Il y a quelques classes vous aide à créer une application avec le  Menu, ils sont énumérés ci dessous:
 
MenuItem

 
CheckMenuItem

 
RadioMenuItem

 
SeparatorMenuItem

2- Exemple JavaFX Menu

Voici un exemple simple de créer un MenuBar avec Menu et Menultem.
MenuDemo.java
package org.o7planning.javafx.menu;

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 MenuDemo extends Application {

    @Override
    public void start(Stage stage) {

        // Create MenuBar
        MenuBar menuBar = new MenuBar();
       
        // Create menus
        Menu fileMenu = new Menu("File");
        Menu editMenu = new Menu("Edit");
        Menu helpMenu = new Menu("Help");
       
        // Create MenuItems
        MenuItem newItem = new MenuItem("New");
        MenuItem openFileItem = new MenuItem("Open File");
        MenuItem exitItem = new MenuItem("Exit");
       
        MenuItem copyItem = new MenuItem("Copy");
        MenuItem pasteItem = new MenuItem("Paste");
       
        // Add menuItems to the Menus
        fileMenu.getItems().addAll(newItem, openFileItem, exitItem);
        editMenu.getItems().addAll(copyItem, pasteItem);
       
        // Add Menus to the MenuBar
        menuBar.getMenus().addAll(fileMenu, editMenu, helpMenu);
       
        BorderPane root = new BorderPane();
        root.setTop(menuBar);
        Scene scene = new Scene(root, 350, 200);
       
        stage.setTitle("JavaFX Menu (o7planning.org)");
        stage.setScene(scene);
        stage.show();
    }

    public static void main(String[] args) {
        Application.launch(args);
    }

}

3- CheckMenuItem, RadioMenuItem et SeparatorMenuItem

De même Button, vous pouvez créer un Menultem avec le texte et l'image. Dans un Menu peut contenir Menultem, CheckMenultem, RadioMenultem ou utiliser SeparatorMenultem pour séparer les élesments 
MenuDemo2.java
package org.o7planning.javafx.menu;

import org.o7planning.javafx.utils.MyImageUtils;

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.CheckMenuItem;
import javafx.scene.control.Menu;
import javafx.scene.control.MenuBar;
import javafx.scene.control.MenuItem;
import javafx.scene.control.RadioMenuItem;
import javafx.scene.control.SeparatorMenuItem;
import javafx.scene.control.ToggleGroup;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.BorderPane;
import javafx.stage.Stage;

public class MenuDemo2 extends Application {

    @Override
    public void start(Stage stage) {

        // Create MenuBar
        MenuBar menuBar = new MenuBar();

        // Create menus
        Menu fileMenu = new Menu("File");
        Menu editMenu = new Menu("Edit");
        Menu projectMenu = new Menu("Project");
        Menu helpMenu = new Menu("Help");

        // Create MenuItems
        MenuItem newItem = new MenuItem("New");
        Image newImage = MyImageUtils.getImage("/org/o7planning/javafx/icon/new-16.png");
        newItem.setGraphic(new ImageView(newImage));

        MenuItem openFileItem = new MenuItem("Open File");
        // SeparatorMenuItem.
        SeparatorMenuItem separator= new SeparatorMenuItem();
        MenuItem exitItem = new MenuItem("Exit");

        MenuItem copyItem = new MenuItem("Copy");
        MenuItem pasteItem = new MenuItem("Paste");
       
        // CheckMenuItem
        CheckMenuItem buildItem = new CheckMenuItem("Build Automatically");
        Image buildImage = MyImageUtils.getImage("/org/o7planning/javafx/icon/build-16.png");
        buildItem.setGraphic(new ImageView(buildImage));
        buildItem.setSelected(true);

        // RadioMenuItem
        RadioMenuItem updateItem1 = new RadioMenuItem("Auto Update");
        RadioMenuItem updateItem2 = new RadioMenuItem("Ask for Update");
       
        ToggleGroup group = new ToggleGroup();
        updateItem1.setToggleGroup(group);
        updateItem2.setToggleGroup(group);
        updateItem1.setSelected(true);
       
       
       
        // Add menuItems to the Menus
        fileMenu.getItems().addAll(newItem, openFileItem,separator, exitItem);
        editMenu.getItems().addAll(copyItem, pasteItem);
        projectMenu.getItems().add(buildItem);
        helpMenu.getItems().addAll(updateItem1,updateItem2);

        // Add Menus to the MenuBar
        menuBar.getMenus().addAll(fileMenu, editMenu, projectMenu, helpMenu);

        BorderPane root = new BorderPane();
        root.setTop(menuBar);
        Scene scene = new Scene(root, 350, 200);

        stage.setTitle("JavaFX Menu (o7planning.org)");
        stage.setScene(scene);
        stage.show();
    }

    public static void main(String[] args) {
        Application.launch(args);
    }

}
MyImageUtils.java
package org.o7planning.javafx.utils;

import java.io.InputStream;

import javafx.scene.image.Image;

public class MyImageUtils {

    // sourcePath: /org/o7planning/javafx/icon/java-16.png
    public static Image getImage(String sourcePath) {
        InputStream input = null;
        try {
            Class<?> c = MyImageUtils.class;
            input = c.getResourceAsStream(sourcePath);
            Image img = new Image(input);
            return img;
        } finally {
            closeQuietly(input);
        }

    }

    private static void closeQuietly(InputStream is) {
        try {
            if (is != null) {
                is.close();
            }
        } catch (Exception e) {

        }
    }
}

4- Événements et raccourcis MenuItem

Avec  MenuItem, vous pouvez gérer les événements lorsque l'utilisateur clique dessous et définir les accélérateurs ( accélérateurs) pour Menultem. L'exemple ci-dessous met les raccourcis Ctrl+X sur Menultem "Exit". L'utilisateur peut appuyer les accourcis Ctrl+X remplacent cliquer Menultem "Exit" pour quitter l'applicatio.
 
// Set Accelerator for Exit MenuItem.
exitItem.setAccelerator(KeyCombination.keyCombination("Ctrl+X"));

// When user click on the Exit item.
exitItem.setOnAction(new EventHandler<ActionEvent>() {

    @Override
    public void handle(ActionEvent event) {
        System.exit(0);
    }
});
Voir l'exemple complet:
MenuEventDemo.java
package org.o7planning.javafx.menu;

import javafx.application.Application;
import javafx.event.ActionEvent;
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.input.KeyCombination;
import javafx.scene.layout.BorderPane;
import javafx.stage.Stage;

public class MenuEventDemo extends Application {

    @Override
    public void start(Stage stage) {

        // Create MenuBar
        MenuBar menuBar = new MenuBar();

        // Create menus
        Menu fileMenu = new Menu("File");
        Menu editMenu = new Menu("Edit");
        Menu helpMenu = new Menu("Help");

        // Create MenuItems
        MenuItem newItem = new MenuItem("New");
        MenuItem openFileItem = new MenuItem("Open File");
        MenuItem exitItem = new MenuItem("Exit");

        // Set Accelerator for Exit MenuItem.
        exitItem.setAccelerator(KeyCombination.keyCombination("Ctrl+X"));

        // When user click on the Exit item.
        exitItem.setOnAction(new EventHandler<ActionEvent>() {

            @Override
            public void handle(ActionEvent event) {
                System.exit(0);
            }
        });

        // Add menuItems to the Menus
        fileMenu.getItems().addAll(newItem, openFileItem, exitItem);

        // Add Menus to the MenuBar
        menuBar.getMenus().addAll(fileMenu, editMenu, helpMenu);

        BorderPane root = new BorderPane();
        root.setTop(menuBar);
        Scene scene = new Scene(root, 350, 200);

        stage.setTitle("JavaFX Menu (o7planning.org)");
        stage.setScene(scene);
        stage.show();
    }

    public static void main(String[] args) {
        Application.launch(args);
    }

}

5- JavaFX ContextMenu

Voyez plus:

View more Tutorials: