package menumnemonicsapp; import com.sun.javafx.runtime.VersionInfo; import javafx.application.Application; import javafx.beans.value.ChangeListener; import javafx.beans.value.ObservableValue; import javafx.collections.ObservableList; import javafx.scene.Scene; import javafx.scene.control.CheckBox; import javafx.scene.control.Menu; import javafx.scene.control.MenuBar; import javafx.scene.control.MenuItem; import javafx.scene.layout.VBox; import javafx.stage.Stage; public class MenuMnemonicsApp extends Application { public static final String CHECK_MENU_PARSE_ID = "check.menu.parse.id"; public static void main(String[] args) { launch(args); } public Scene getScene() { return new MnemonicsScene(); } protected VBox box = new VBox(10); @Override public void start(Stage primaryStage) throws Exception { primaryStage.setTitle(VersionInfo.getRuntimeVersion()); primaryStage.setScene(getScene()); primaryStage.show(); } public class MnemonicsScene extends Scene { public MnemonicsScene() { super(box, 600, 400); final MenuBar bar = new MenuBar(); generateMenus(bar.getMenus(), 2, 2, 0); CheckBox check = new CheckBox("setMnemonicParse()"); check.setSelected(true); check.setId(CHECK_MENU_PARSE_ID); check.selectedProperty().addListener(new ChangeListener() { public void changed(ObservableValue ov, Boolean t, Boolean t1) { setMnemocicParse((ObservableList)bar.getMenus(), t1); } }); box.getChildren().addAll(bar, check); } void setMnemocicParse(ObservableList list, Boolean value) { for (MenuItem item : list) { if (item instanceof Menu) { ((Menu)item).setMnemonicParsing(value); setMnemocicParse(((Menu)item).getItems(), value); } } } char letter = 'A'; void generateMenus(ObservableList list, int width, int depth, int level) { for (int i = 0; i < width; i++) { String name = "Menu " + level + " " + i + " _" + letter++; if (level < depth) { Menu menu = new Menu(name); generateMenus(menu.getItems(), width, depth, level + 1); list.add(menu); } else { MenuItem menu = new MenuItem(name); list.add(menu); } } } } }