javafx.scene.control.MenuButton Maven / Gradle / Ivy
/*
* Copyright (c) 2010, 2017, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
package javafx.scene.control;
import javafx.css.PseudoClass;
import javafx.beans.property.ObjectProperty;
import javafx.beans.property.ObjectPropertyBase;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.event.Event;
import javafx.event.EventHandler;
import javafx.event.EventType;
import javafx.geometry.Side;
import javafx.scene.AccessibleAction;
import javafx.scene.AccessibleRole;
import javafx.scene.Node;
import javafx.scene.control.skin.MenuButtonSkin;
import javafx.beans.property.ReadOnlyBooleanProperty;
import javafx.beans.property.ReadOnlyBooleanWrapper;
/**
* MenuButton is a button which, when clicked or pressed, will show a
* {@link ContextMenu}. A MenuButton shares a very similar API to the {@link Menu}
* control, insofar that you set the items that should be shown in the
* {@link #getItems() items} ObservableList, and there is a {@link #textProperty() text} property to specify the
* label shown within the MenuButton.
*
* As mentioned, like the Menu API itself, you'll find an {@link #getItems() items} ObservableList
* within which you can provide anything that extends from {@link MenuItem}.
* There are several useful subclasses of {@link MenuItem} including
* {@link RadioMenuItem}, {@link CheckMenuItem}, {@link Menu},
* {@link SeparatorMenuItem} and {@link CustomMenuItem}.
*
* A MenuButton can be set to show its menu on any side of the button. This is
* specified using the {@link #popupSideProperty() popupSide} property. By default
* the menu appears below the button. However, regardless of the popupSide specified,
* if there is not enough room, the {@link ContextMenu} will be
* smartly repositioned, most probably to be on the opposite side of the
* MenuButton.
*
*
Example:
*
* MenuButton m = new MenuButton("Eats");
* m.getItems().addAll(new MenuItem("Burger"), new MenuItem("Hot Dog"));
*
*
*
* MnemonicParsing is enabled by default for MenuButton.
*
*
* @see MenuItem
* @see Menu
* @see SplitMenuButton
* @since JavaFX 2.0
*/
public class MenuButton extends ButtonBase {
/***************************************************************************
* *
* Static properties and methods *
* *
**************************************************************************/
/**
* Called prior to the MenuButton showing its popup after the user
* has clicked or otherwise interacted with the MenuButton.
* @since JavaFX 8u60
*/
public static final EventType ON_SHOWING =
new EventType(Event.ANY, "MENU_BUTTON_ON_SHOWING");
/**
* Called after the MenuButton has shown its popup.
* @since JavaFX 8u60
*/
public static final EventType ON_SHOWN =
new EventType(Event.ANY, "MENU_BUTTON_ON_SHOWN");
/**
* Called when the MenuButton popup will be hidden.
* @since JavaFX 8u60
*/
public static final EventType ON_HIDING =
new EventType(Event.ANY, "MENU_BUTTON_ON_HIDING");
/**
* Called when the MenuButton popup has been hidden.
* @since JavaFX 8u60
*/
public static final EventType ON_HIDDEN =
new EventType(Event.ANY, "MENU_BUTTON_ON_HIDDEN");
/***************************************************************************
* *
* Constructors *
* *
**************************************************************************/
/**
* Creates a new empty menu button. Use {@link #setText(String)},
* {@link #setGraphic(Node)} and {@link #getItems()} to set the content.
*/
public MenuButton() {
this(null, null);
}
/**
* Creates a new empty menu button with the given text to display on the
* button. Use {@link #setGraphic(Node)} and {@link #getItems()} to set the
* content.
*
* @param text the text to display on the menu button
*/
public MenuButton(String text) {
this(text, null);
}
/**
* Creates a new empty menu button with the given text and graphic to
* display on the button. Use {@link #getItems()} to set the content.
*
* @param text the text to display on the menu button
* @param graphic the graphic to display on the menu button
*/
public MenuButton(String text, Node graphic) {
this(text, graphic, (MenuItem[])null);
}
/**
* Creates a new menu button with the given text and graphic to
* display on the button, and inserts the given items
* into the {@link #getItems() items} list.
*
* @param text the text to display on the menu button
* @param graphic the graphic to display on the menu button
* @param items The items to display in the popup menu.
* @since JavaFX 8u40
*/
public MenuButton(String text, Node graphic, MenuItem... items) {
if (text != null) {
setText(text);
}
if (graphic != null) {
setGraphic(graphic);
}
if (items != null) {
getItems().addAll(items);
}
getStyleClass().setAll(DEFAULT_STYLE_CLASS);
setAccessibleRole(AccessibleRole.MENU_BUTTON);
setMnemonicParsing(true); // enable mnemonic auto-parsing by default
// the default value for popupSide = Side.BOTTOM therefor
// PSEUDO_CLASS_OPENVERTICALLY should be set from the start.
pseudoClassStateChanged(PSEUDO_CLASS_OPENVERTICALLY, true);
}
/***************************************************************************
* *
* Properties *
* *
**************************************************************************/
private final ObservableList