/* * Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved. */ package helloworld; import javafx.application.Application; import javafx.beans.InvalidationListener; import javafx.beans.Observable; import javafx.event.ActionEvent; import javafx.event.EventHandler; import javafx.scene.Group; import javafx.scene.Scene; import javafx.scene.control.Button; import javafx.scene.control.ContextMenu; import javafx.scene.control.Label; import javafx.scene.control.Menu; import javafx.scene.control.MenuBar; import javafx.scene.control.MenuItem; import javafx.scene.control.Slider; import javafx.scene.control.ToggleButton; import javafx.scene.control.MenuButton; import javafx.scene.control.SplitMenuButton; import javafx.scene.input.KeyCode; import javafx.scene.input.KeyCodeCombination; import javafx.scene.input.KeyCombination; import javafx.scene.input.KeyEvent; import javafx.scene.input.MouseEvent; import javafx.scene.paint.Color; import javafx.stage.Stage; import javafx.stage.WindowEvent; /** * @author mickf */ public class HelloMnemonicModified extends Application { /** * @param args the command line arguments */ public static void main(String[] args) { Application.launch(args); } Scene scene; final MenuBar menuBar = new MenuBar(); @Override public void start(Stage stage) { stage.setTitle("Hello Mnemonic"); scene = new Scene(new Group(), 600, 450); scene.setFill(Color.CHOCOLATE); KeyCodeCombination acceleratorKeyComboA = new KeyCodeCombination(KeyCode.A, KeyCombination.CONTROL_DOWN, KeyCombination.ALT_DOWN); KeyCodeCombination acceleratorKeyComboC = new KeyCodeCombination(KeyCode.C, KeyCombination.CONTROL_DOWN, KeyCombination.ALT_DOWN); KeyCodeCombination acceleratorKeyComboB = new KeyCodeCombination(KeyCode.B, KeyCombination.CONTROL_DOWN); stage.setTitle("Hello Mnemonic"); scene = new Scene(new Group(), 600, 450); menuBar.setPrefWidth(scene.getWidth()); final Menu menu = new Menu("_File"); menu.setMnemonicParsing(true); MenuItem menuitem1 = new MenuItem(); menuitem1.setText("_Open"); menuitem1.setMnemonicParsing(true); menuitem1.setAccelerator((KeyCombination) acceleratorKeyComboA); menuitem1.setOnAction(new EventHandler() { @Override public void handle(ActionEvent e) { System.out.println("menuitem1 got Event: " + e); } }); MenuItem menuitem2 = new MenuItem(); menuitem2.setText("Save"); menuitem2.setAccelerator((KeyCombination) acceleratorKeyComboB); menuitem2.setOnAction(new EventHandler() { @Override public void handle(ActionEvent e) { System.out.println("menuitem2 got Event: " + e); } }); MenuItem menuitem3 = new MenuItem(); menuitem3.setText("_Underline"); menuitem3.setMnemonicParsing(false); MenuItem menuitem4 = new MenuItem(); menuitem4.setText("Exit"); menu.getItems().clear(); // menu.getItems().addAll(menuitem1, menuitem2, menuitem3, menuitem4); Menu menu2 = new Menu("_Edit"); MenuItem menuitem6 = new MenuItem(); menuitem6.setText("Cut"); MenuItem menuitem7 = new MenuItem(); menuitem7.setText("Copy"); menuitem7.setAccelerator((KeyCombination) acceleratorKeyComboC); menuitem7.setOnAction(new EventHandler() { @Override public void handle(ActionEvent e) { System.out.println("menuitem7 got Event: " + e); } }); MenuItem menuitem8 = new MenuItem(); menuitem8.setText("Paste"); menu2.getItems().clear(); menu2.getItems().addAll(menuitem6, menuitem7, menuitem8); menu.getItems().addAll(menuitem1, menuitem2, menuitem3, menuitem4, menu2); menuBar.getMenus().clear(); // menuBar.getMenus().addAll(menu, menu2); menuBar.getMenus().addAll(menu); scene.widthProperty().addListener(new InvalidationListener() { public void invalidated(Observable ov) { menuBar.setPrefWidth(scene.getWidth()); } }); ((Group)scene.getRoot()).getChildren().clear(); ((Group)scene.getRoot()).getChildren().addAll(menuBar); Button buttonMnemonic = new Button(); buttonMnemonic.setText("Mnemonic _Me@me"); buttonMnemonic.setDefaultButton(true); buttonMnemonic.setLayoutX(25); buttonMnemonic.setLayoutY(100); buttonMnemonic.setOnAction(new EventHandler() { @Override public void handle(ActionEvent e) { // System.out.println("Mnemonic got Event: " + e); } }); buttonMnemonic.addEventHandler(MouseEvent.MOUSE_PRESSED, new EventHandler() { @Override public void handle(MouseEvent e) { // System.out.println("Mnemonic got MouseEvent: " + e); } }); buttonMnemonic.addEventHandler(KeyEvent.KEY_PRESSED, new EventHandler() { @Override public void handle(KeyEvent e) { // System.out.println("Mnemonic got KeyEvent: " + e); } }); ((Group)scene.getRoot()).getChildren().add(buttonMnemonic); Button buttonNormal = new Button(); buttonNormal.setText("_Normal Me @Ctrl+N"); buttonNormal.setLayoutX(25); buttonNormal.setLayoutY(140); buttonNormal.setOnAction(new EventHandler() { @Override public void handle(ActionEvent e) { System.out.println("Normal got Event: " + e); } }); ((Group)scene.getRoot()).getChildren().add(buttonNormal); final ContextMenu popupMenu = new ContextMenu(); popupMenu.setOnShowing(new EventHandler() { @Override public void handle(WindowEvent t) { System.out.println("showing"); } }); popupMenu.setOnShown(new EventHandler() { @Override public void handle(WindowEvent t) { System.out.println("shown"); } }); MenuItem item1 = new MenuItem("_About"); item1.setOnAction(new EventHandler() { @Override public void handle(ActionEvent e) { System.out.println("About got Event: " + e); } }); MenuItem item2 = new MenuItem("P_References"); item2.setOnAction(new EventHandler() { @Override public void handle(ActionEvent e) { System.out.println("Preferences got Event: " + e); } }); MenuItem item3 = new MenuItem("Templates"); item3.setOnAction(new EventHandler() { @Override public void handle(ActionEvent e) { System.out.println("Templates got Event: " + e); } }); popupMenu.getItems().add(item1); popupMenu.getItems().add(item2); popupMenu.getItems().add(item3); final Button buttonPopup = new Button("_Popup"); buttonPopup.setContextMenu(popupMenu); buttonPopup.setOnAction(new EventHandler() { @Override public void handle(ActionEvent e) { System.out.println("Popup got Event: " + e); } }); buttonPopup.setLayoutX(25); buttonPopup.setLayoutY(180); ((Group)scene.getRoot()).getChildren().add(buttonPopup); final ToggleButton toggleButton = new ToggleButton("_Toggle"); toggleButton.setLayoutX(25); toggleButton.setLayoutY(220); toggleButton.addEventHandler(ActionEvent.ACTION, new EventHandler() { @Override public void handle(ActionEvent e) { System.out.println("Toggle got Event: " + e); } }); ((Group)scene.getRoot()).getChildren().add(toggleButton); final MenuButton menuButton = new MenuButton("Menu_Button"); menuButton.getItems().add(new MenuItem("Item 1")); menuButton.getItems().add(new MenuItem("Item 2")); menuButton.setLayoutX(25); menuButton.setLayoutY(260); menuButton.addEventHandler(ActionEvent.ACTION, new EventHandler() { @Override public void handle(ActionEvent e) { System.out.println("MenuButton got Event: " + e); } }); ((Group)scene.getRoot()).getChildren().add(menuButton); final SplitMenuButton splitMenuButton = new SplitMenuButton(new MenuItem("_X"), new MenuItem("_Y")); splitMenuButton.setText("Sp_litMenuButton"); splitMenuButton.setLayoutX(25); splitMenuButton.setLayoutY(300); splitMenuButton.addEventHandler(ActionEvent.ACTION, new EventHandler() { @Override public void handle(ActionEvent e) { System.out.println("SplitMenuButton got Event: " + e); } }); ((Group)scene.getRoot()).getChildren().add(splitMenuButton); final Button buttonDuplicate1 = new Button("_Duplicate1"); buttonDuplicate1.setLayoutX(200); buttonDuplicate1.setLayoutY(80); buttonDuplicate1.setOnAction(new EventHandler() { @Override public void handle(ActionEvent e) { System.out.println("Duplicate1 got Event: " + e); } }); ((Group)scene.getRoot()).getChildren().add(buttonDuplicate1); final Button buttonDuplicate2 = new Button("_Duplicate2"); buttonDuplicate2.setLayoutX(300); buttonDuplicate2.setLayoutY(80); buttonDuplicate2.setOnAction(new EventHandler() { @Override public void handle(ActionEvent e) { System.out.println("Duplicate2 got Event: " + e); } }); ((Group)scene.getRoot()).getChildren().add(buttonDuplicate2); final Button buttonDuplicate3 = new Button("_Duplicate3"); buttonDuplicate3.setLayoutX(400); buttonDuplicate3.setLayoutY(80); buttonDuplicate3.setOnAction(new EventHandler() { @Override public void handle(ActionEvent e) { System.out.println("Duplicate3 got Event: " + e); } }); ((Group)scene.getRoot()).getChildren().add(buttonDuplicate3); final Slider slider = new Slider(); slider.setLayoutX(200); slider.setLayoutY(230); Label sliderLabel = new Label("_Slider"); sliderLabel.setMnemonicParsing(true); sliderLabel.setLabelFor(slider); sliderLabel.setLayoutX(200); sliderLabel.setLayoutY(210); slider.addEventHandler(ActionEvent.ACTION, new EventHandler() { @Override public void handle(ActionEvent e) { System.out.println("Slider got Event: " + e); slider.setValue(slider.getValue()+1); } }); ((Group)scene.getRoot()).getChildren().add(slider); ((Group)scene.getRoot()).getChildren().add(sliderLabel); Label noAccelerator = new Label("Label with @Ctrl+Z.. no accelerator"); noAccelerator.setLayoutX(5); noAccelerator.setLayoutY(35); noAccelerator.addEventHandler(ActionEvent.ACTION, new EventHandler() { @Override public void handle(ActionEvent e) { System.out.println("whoops, noAccelerator got Event: " + e); } }); ((Group)scene.getRoot()).getChildren().add(noAccelerator); Label noMnemonic = new Label("Label with _Underline"); noMnemonic.setLayoutX(205); noMnemonic.setLayoutY(35); noMnemonic.addEventHandler(ActionEvent.ACTION, new EventHandler() { @Override public void handle(ActionEvent e) { System.out.println("Whoops, Label got Event: " + e); } }); ((Group)scene.getRoot()).getChildren().add(noMnemonic); stage.setScene(scene); stage.show(); } }