/* * Copyright (c) 2012, Oracle and/or its affiliates. All rights reserved. */ package tests; import java.util.ArrayList; import java.util.List; import javafx.application.Application; import javafx.event.Event; import javafx.event.EventHandler; import javafx.scene.Group; import javafx.scene.Scene; import javafx.scene.control.*; import javafx.stage.Stage; /** * * @author slions */ public class TestMenus extends Application { @Override public void start(Stage stage) { stage.setTitle("Sample"); final Scene scene = new Scene(new Group(), 800, 500); /////////////////////////////////////// final Menu mainMenu = new Menu("Main"); final Menu menu3 = new Menu("Menu 3"); final Menu menu31 = new Menu("Menu 31"); final MenuItem menu311 = new MenuItem("Menu 311"); final MenuItem menu312 = new MenuItem("Menu 312"); final List list31 = new ArrayList<>(); list31.add(menu311); list31.add(menu312); menu31.getItems().addAll(menu311, menu312); final Menu menu32 = new Menu("Menu 32"); final MenuItem menu321 = new MenuItem("Menu 321"); final MenuItem menu322 = new MenuItem("Menu 322"); final List list32 = new ArrayList<>(); list32.add(menu321); list32.add(menu322); // Uncomment to workaround the issue // menu32.getItems().addAll(menu321, menu322); menu3.getItems().addAll(menu31, menu32); mainMenu.getItems().addAll(menu3); mainMenu.setOnMenuValidation(new EventHandler() { @Override public void handle(Event t) { menu31.getItems().setAll(list31); menu32.getItems().setAll(list32); menu31.setDisable(false); menu32.setDisable(false); } }); final MenuBar menuBar = new MenuBar(); menuBar.getMenus().add(mainMenu); menuBar.setUseSystemMenuBar(true); final Group root = (Group) scene.getRoot(); root.getChildren().clear(); root.getChildren().addAll(menuBar); stage.setScene(scene); stage.show(); } /** * @param args the command line arguments */ public static void main(String[] args) { Application.launch(args); } }