Java examples for JavaFX:Menu
create JavaFX Menu Bar
/*// w w w. j av a 2 s .co m * Copyright (c) 2008, 2013, Oracle and/or its affiliates. * All rights reserved. Use is subject to license terms. * * This file is available and licensed under the following license: * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * * - Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * - Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in * the documentation and/or other materials provided with the distribution. * - Neither the name of Oracle Corporation nor the names of its * contributors may be used to endorse or promote products derived * from this software without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ //package com.java2s; import java.util.ArrayList; import java.util.List; import javafx.application.Platform; import javafx.beans.InvalidationListener; import javafx.beans.Observable; import javafx.css.PseudoClass; import javafx.scene.Node; import javafx.scene.control.CheckMenuItem; import javafx.scene.control.CheckMenuItemBuilder; import javafx.scene.control.Menu; import javafx.scene.control.MenuBar; import javafx.scene.control.MenuItem; import javafx.scene.control.RadioMenuItemBuilder; import javafx.scene.control.SeparatorMenuItem; import javafx.scene.input.KeyCharacterCombination; import javafx.scene.input.KeyCombination; public class Main { static MenuBar createMenuBar() { final MenuBar mb = new MenuBar(); mb.getMenus().addAll(createMenu("File"), createMenu("Edit"), createMenu("View"), createMenu("Help")); // mb.setMouseTransparent(true); Platform.runLater(new Runnable() { @Override public void run() { // get second menu and force into hover state try { new ArrayList<Node>(mb.lookupAll(".menu")).get(1) .pseudoClassStateChanged( PseudoClass.getPseudoClass("hover"), true); } catch (Exception e) { e.printStackTrace(); } } }); return mb; } static Menu createMenu(String name) { Menu m = new Menu(name); m.getStyleClass().add(name); m.getItems().addAll(createMenuContents()); return m; } static MenuItem[] createMenuContents() { List<MenuItem> menuItems = new ArrayList<>(); // Menu menu11 = makeMenu("_New", new ImageView(new Image(getClass().getResourceAsStream("about_16.png")))); // final Menu menu11 = new Menu("_New", new ImageView(new Image("helloworld/about_16.png"))); // MenuItem menu12 = new MenuItem("_Open", new ImageView(new Image("helloworld/folder_16.png"))); final Menu menu11 = new Menu("_New"); MenuItem menu12 = new MenuItem("_Open"); menu12.getStyleClass().add("OpenMenuItem"); menu12.setAccelerator(new KeyCharacterCombination("]", KeyCombination.SHIFT_DOWN, KeyCombination.META_DOWN)); Menu menu13 = new Menu("_Submenu"); // CheckMenuItem showMessagesItem = new CheckMenuItem("Enable onShowing/onHiding _messages", // new ImageView(new Image("helloworld/about_16.png"))); CheckMenuItem showMessagesItem = new CheckMenuItem( "Enable onShowing/onHiding _messages"); MenuItem menu15 = new MenuItem("E_xit"); final String change[] = { "Change Text", "Change Back" }; final MenuItem menu16 = new MenuItem(change[0]); final boolean toggle = false; menu16.setAccelerator(KeyCombination.keyCombination("Shortcut+C")); menuItems.add(menu11); menuItems.add(menu12); menuItems.add(menu13); // menuItems.add(showMessagesItem); menuItems.add(menu16); menuItems.add(new SeparatorMenuItem()); menuItems.add(CheckMenuItemBuilder.create().text("Check").build()); menuItems.add(CheckMenuItemBuilder.create().text("Check Selected") .selected(true).build()); menuItems.add(new SeparatorMenuItem()); menuItems.add(RadioMenuItemBuilder.create().text("Radio").build()); menuItems.add(RadioMenuItemBuilder.create().text("Radio Selected") .selected(true).build()); menuItems.add(new SeparatorMenuItem()); menuItems.add(menu15); // --- Menu 11 submenu final MenuItem menu111 = new MenuItem("blah"); final MenuItem menu112 = new MenuItem("foo"); final CheckMenuItem menu113 = new CheckMenuItem("Show \"foo\" item"); menu113.setSelected(true); menu113.selectedProperty().addListener(new InvalidationListener() { @Override public void invalidated(Observable valueModel) { menu112.setVisible(menu113.isSelected()); System.err.println("MenuItem \"foo\" is now " + (menu112.isVisible() ? "" : "not") + " visible."); } }); menu11.getItems().addAll(menu111, menu112, menu113); // --- Menu 13 submenu MenuItem menu131 = new MenuItem("Item _1"); MenuItem menu132 = new MenuItem("Item _2"); menu13.getItems().addAll(menu131, menu132); return menuItems.toArray(new MenuItem[menuItems.size()]); } }