/* * Copyright (c) 2010, 2011, Oracle and/or its affiliates. All rights reserved. */ package tests; import javafx.application.Application; import javafx.event.ActionEvent; import javafx.event.Event; import javafx.event.EventHandler; import javafx.scene.Group; import javafx.scene.Scene; import javafx.scene.control.*; import javafx.scene.input.KeyCode; import javafx.scene.input.KeyCodeCombination; import javafx.scene.input.KeyCombination; import javafx.stage.Stage; public class Test extends Application { int count = 1; final MenuItem actionMenuItem = new MenuItem("Action"); final MenuItem undoMenuItem = new MenuItem("Undo"); final MenuItem redoMenuItem = new MenuItem("Redo"); @Override public void start(Stage stage) { stage.setTitle("Sample"); final Scene scene = new Scene(new Group(), 500, 500); final Group root = (Group) scene.getRoot(); final Button button = new Button("Button"); button.setLayoutX(100); button.setLayoutY(100); final MenuBar menuBar = new MenuBar(); menuBar.setUseSystemMenuBar(true); final Menu menu = new Menu("Edit menu"); actionMenuItem.setAccelerator(new KeyCodeCombination(KeyCode.A, KeyCombination.META_DOWN)); actionMenuItem.setOnAction(new EventHandler() { @Override public void handle(ActionEvent event) { System.out.println("=== action invoked"); } }); undoMenuItem.setAccelerator(new KeyCodeCombination(KeyCode.Z, KeyCombination.META_DOWN)); redoMenuItem.setAccelerator(new KeyCodeCombination(KeyCode.Z, KeyCombination.SHIFT_DOWN, KeyCombination.META_DOWN)); menu.getItems().addAll(actionMenuItem, undoMenuItem, redoMenuItem); menuBar.getMenus().add(menu); menu.setOnMenuValidation(new EventHandler() { @Override public void handle(Event event) { System.out.println("=== onMenuValidation handler invoked"); undoMenuItem.setText("Undo " + count++); } }); root.getChildren().clear(); root.getChildren().addAll(menuBar, button); stage.setScene(scene); stage.show(); } /** * @param args the command line arguments */ public static void main(String[] args) { Application.launch(args); } }