import javafx.application.Application; import javafx.event.*; import javafx.scene.Scene; import javafx.scene.control.*; import javafx.scene.input.*; import javafx.scene.layout.VBox; import javafx.stage.Stage; public class MenuExample extends Application { private MenuItem openItem, closeItem, newItem; private VBox aPane; private ContextMenu popupMenu; public void start(Stage primaryStage) { aPane = new VBox(); Scene scene = new Scene(aPane, 300, 100); // Set window size // Create the File menu Menu fileMenu = new Menu("_File"); newItem = new MenuItem("New"); newItem.setAccelerator(KeyCombination.keyCombination("Ctrl+N")); openItem = new MenuItem("Open..."); openItem.setAccelerator(KeyCombination.keyCombination("Ctrl+O")); closeItem = new MenuItem("Close"); closeItem.setAccelerator(KeyCombination.keyCombination("Ctrl+C")); closeItem.setDisable(true); MenuItem saveAsItem = new MenuItem("Save As..."); saveAsItem.setAccelerator(KeyCombination.keyCombination("Ctrl+S")); fileMenu.getItems().addAll(newItem, new SeparatorMenuItem(), openItem, closeItem, saveAsItem); // Set up the event handlers for the File menu newItem.setOnAction(new EventHandler() { public void handle(ActionEvent e) { closeItem.setDisable(false); newItem.setDisable(true); openItem.setDisable(true); System.out.println("NEW has been pressed"); } }); openItem.setOnAction(new EventHandler() { public void handle(ActionEvent e) { closeItem.setDisable(false); openItem.setDisable(true); newItem.setDisable(true); System.out.println("OPEN has been pressed"); } }); closeItem.setOnAction(new EventHandler() { public void handle(ActionEvent e) { openItem.setDisable(false); newItem.setDisable(false); closeItem.setDisable(true); System.out.println("CLOSE has been pressed"); } }); saveAsItem.setOnAction(new EventHandler() { public void handle(ActionEvent e) { System.out.println("SAVE AS has been pressed"); } }); //Create the Search menu Menu searchMenu = new Menu("Search"); MenuItem findItem = new MenuItem("Find"); MenuItem replaceItem = new MenuItem("Replace"); searchMenu.getItems().addAll(findItem, replaceItem); // Set up the event handlers for the Search menu findItem.setOnAction(new EventHandler() { public void handle(ActionEvent e) { System.out.println("FIND has been pressed"); } }); replaceItem.setOnAction(new EventHandler() { public void handle(ActionEvent e) { System.out.println("REPLACE has been pressed"); } }); // Create the Edit menu Menu editMenu = new Menu("_Edit"); CheckMenuItem gridItem = new CheckMenuItem("Use GridLines"); MenuItem copyItem = new MenuItem("Copy"); editMenu.getItems().addAll(gridItem, new SeparatorMenuItem(), copyItem, searchMenu); // Set up the event handlers for the Edit menu gridItem.setOnAction(new EventHandler() { public void handle(ActionEvent e) { if (((CheckMenuItem)e.getSource()).isSelected()) System.out.println("USE GRIDLINES has been selected"); else System.out.println("USE GRIDLINES has been unselected"); } }); copyItem.setOnAction(new EventHandler() { public void handle(ActionEvent e) { System.out.println("COPY has been pressed"); } }); // Create the Settings menu Menu settingsMenu = new Menu("_Settings"); ToggleGroup settingGroup = new ToggleGroup(); RadioMenuItem smallItem = new RadioMenuItem("Small"); smallItem.setToggleGroup(settingGroup); RadioMenuItem mediumItem = new RadioMenuItem("Medium"); mediumItem.setToggleGroup(settingGroup); RadioMenuItem largeItem = new RadioMenuItem("Large"); largeItem.setToggleGroup(settingGroup); settingsMenu.getItems().addAll(smallItem, mediumItem, largeItem); // Set up the event handlers for the Settings menu smallItem.setOnAction(new EventHandler() { public void handle(ActionEvent e) { System.out.println("SMALL has been selected"); } }); mediumItem.setOnAction(new EventHandler() { public void handle(ActionEvent e) { System.out.println("MEDIUM has been selected"); } }); largeItem.setOnAction(new EventHandler() { public void handle(ActionEvent e) { System.out.println("LARGE has been selected"); } }); // Create a Contect (or popup) menu popupMenu = new ContextMenu(); MenuItem helpItem = new MenuItem("Help"); MenuItem inspectItem = new MenuItem("Inspect"); popupMenu.getItems().addAll(helpItem, inspectItem); aPane.addEventHandler(MouseEvent.MOUSE_CLICKED, new EventHandler() { public void handle(MouseEvent e) { if (e.getButton() == MouseButton.SECONDARY) popupMenu.show(aPane, e.getScreenX()-50, e.getScreenY()-25); } }); // Set up the event handlers for the Popup menu helpItem.setOnAction(new EventHandler() { public void handle(ActionEvent e) { System.out.println("HELP has been pressed"); } }); inspectItem.setOnAction(new EventHandler() { public void handle(ActionEvent e) { System.out.println("INSPECT has been pressed"); } }); // Add the menus to a menubar and then add the menubar to the pane MenuBar menuBar = new MenuBar(); menuBar.getMenus().addAll(fileMenu, editMenu, settingsMenu); aPane.getChildren().add(menuBar); primaryStage.setTitle("Menu Example"); primaryStage.setScene(scene); primaryStage.show(); } public static void main(String[] args) { launch(args); } }