1 /*
   2  * Copyright (c) 2010, 2014, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package hello;
  27 
  28 import javafx.application.Application;
  29 import javafx.geometry.Side;
  30 import javafx.scene.Node;
  31 import javafx.scene.Scene;
  32 import javafx.scene.control.Button;
  33 import javafx.scene.control.CheckBox;
  34 import javafx.scene.control.Label;
  35 import javafx.scene.control.MenuButton;
  36 import javafx.scene.control.MenuItem;
  37 import javafx.scene.control.RadioButton;
  38 import javafx.scene.control.SplitMenuButton;
  39 import javafx.scene.control.ToggleGroup;
  40 import javafx.scene.control.Tooltip;
  41 import javafx.scene.layout.HBox;
  42 import javafx.scene.layout.VBox;
  43 import javafx.scene.paint.Color;
  44 import javafx.stage.Stage;
  45 import static javafx.geometry.NodeOrientation.*;
  46 
  47 
  48 public class HelloMenuButton extends Application {
  49 
  50     /**
  51      * @param args the command line arguments
  52      */
  53     public static void main(String[] args) {
  54         Application.launch(args);
  55     }
  56 
  57     @Override public void start(Stage stage) {
  58         stage.setTitle("Hello MenuButton");
  59 
  60         VBox root = new VBox(20);
  61         root.setStyle("-fx-padding: 30;");
  62 
  63         Scene scene = new Scene(root);
  64         scene.setFill(Color.CHOCOLATE);
  65 
  66         final VBox vBox1 = new VBox(10);
  67 
  68 
  69         /***************************************************************
  70          *                                                             *
  71          * Simple button for comparison with the others                *
  72          *                                                             *
  73          **************************************************************/
  74 
  75         Button button = new Button("Simple Button");
  76         button.setTooltip(new Tooltip("Tooltip for Simple Button"));
  77         button.setOnAction(e -> System.out.println("Simple Button"));
  78         vBox1.getChildren().add(button);
  79 
  80 
  81         /***************************************************************
  82          *                                                             *
  83          * Simple menu button                                          *
  84          *                                                             *
  85          **************************************************************/
  86 
  87         MenuButton mb = new MenuButton("MenuButton");
  88         mb.setTooltip(new Tooltip("Tooltip for MenuButton"));
  89 
  90         final MenuItem coke = new MenuItem("Coke");
  91         coke.setOnAction(e -> System.out.println(coke.getText()));
  92         mb.getItems().add(coke);
  93 
  94         final MenuItem pepsi = new MenuItem("Pepsi");
  95         pepsi.setOnAction(e -> System.out.println(pepsi.getText()));
  96         mb.getItems().add(pepsi);
  97         mb.getItems().addAll(new MenuItem("Foo"),
  98                              new MenuItem("Foo"),
  99                              new MenuItem("Foo"),
 100                              new MenuItem("Foo"),
 101                              new MenuItem("Foo"),
 102                              new MenuItem("Foo"),
 103                              new MenuItem("Foo"),
 104                              new MenuItem("Foo"),
 105                              new MenuItem("Foo"),
 106                              new MenuItem("Foo"),
 107                              new MenuItem("Foo"),
 108                              new MenuItem("Foo"));
 109         vBox1.getChildren().add(mb);
 110 
 111 
 112 
 113 
 114         /***************************************************************
 115          *                                                             *
 116          * Split menu button                                           *
 117          *                                                             *
 118          **************************************************************/
 119 
 120         SplitMenuButton smb = new SplitMenuButton();
 121         smb.setText("SplitMenuButton1");
 122         smb.setTooltip(new Tooltip("Tooltip for SplitMenuButton1"));
 123         smb.setOnAction(e -> System.out.println("SplitMenuButton1"));
 124 
 125         MenuItem mi = new MenuItem("Divide");
 126         mi.setOnAction(e -> System.out.println("Divide"));
 127         smb.getItems().add(mi);
 128 
 129         mi = new MenuItem("Conquer");
 130         mi.setOnAction(e -> System.out.println("Conquer"));
 131         smb.getItems().add(mi);
 132 
 133         vBox1.getChildren().add(smb);
 134 
 135 
 136 
 137         /***************************************************************
 138          *                                                             *
 139          * Split menu button that                                      *
 140          * gets label and action from selected item.                   *
 141          *                                                             *
 142          **************************************************************/
 143 
 144         final SplitMenuButton smb3 = new SplitMenuButton();
 145         smb3.setTooltip(new Tooltip("Tooltip for SplitMenuButton2"));
 146         smb3.setOnAction(e -> System.out.println("SplitMenuButton2"));
 147 
 148         {
 149             final MenuItem menuItem = new MenuItem("Land");
 150             menuItem.setOnAction(e -> {
 151                 System.out.println("Land");
 152                 smb3.setText(menuItem.getText());
 153                 smb3.setOnAction(menuItem.getOnAction());
 154             });
 155             smb3.getItems().add(menuItem);
 156         }
 157 
 158         {
 159             final MenuItem menuItem = new MenuItem("Sea");
 160             menuItem.setOnAction(e -> {
 161                 System.out.println("Sea");
 162                 smb3.setText(menuItem.getText());
 163                 smb3.setOnAction(menuItem.getOnAction());
 164             });
 165             smb3.getItems().add(menuItem);
 166         }
 167 
 168         smb3.setText(smb3.getItems().get(0).getText());
 169         smb3.setOnAction(smb3.getItems().get(0).getOnAction());
 170 
 171         vBox1.getChildren().add(smb3);
 172 
 173 
 174         VBox vBox2 = new VBox(10);
 175 
 176         {
 177             HBox hBox = new HBox(14);
 178             hBox.getChildren().add(new Label("Popup Side:"));
 179 
 180             ToggleGroup toggleGroup = new ToggleGroup();
 181             for (final Side side : Side.class.getEnumConstants()) {
 182                 final RadioButton rb = new RadioButton(side.toString());
 183                 rb.selectedProperty().addListener(valueModel -> {
 184                     for (Node node : vBox1.getChildren()) {
 185                         if (node instanceof MenuButton) {
 186                             ((MenuButton)node).setPopupSide(side);
 187                         }
 188                     }
 189                 });
 190                 rb.setToggleGroup(toggleGroup);
 191                 if (side == Side.BOTTOM) {
 192                     rb.setSelected(true);
 193                 }
 194                 hBox.getChildren().add(rb);
 195             }
 196             vBox2.getChildren().add(hBox);
 197         }
 198 
 199         {
 200             HBox hBox = new HBox(10);
 201 
 202             {
 203                 final CheckBox cb = new CheckBox("Disable");
 204                 cb.selectedProperty().addListener(valueModel -> {
 205                     boolean disabled = cb.isSelected();
 206                     for (Node node : vBox1.getChildren()) {
 207                         node.setDisable(disabled);
 208                     }
 209                 });
 210                 hBox.getChildren().addAll(cb);
 211             }
 212 
 213             {
 214                 final CheckBox cb = new CheckBox("RTL");
 215                 cb.selectedProperty().addListener(valueModel -> {
 216                     boolean rtl = cb.isSelected();
 217                     for (Node node : vBox1.getChildren()) {
 218                         node.setNodeOrientation(rtl ? RIGHT_TO_LEFT : LEFT_TO_RIGHT);
 219                     }
 220                 });
 221                 cb.setSelected(scene.getEffectiveNodeOrientation() == RIGHT_TO_LEFT);
 222                 hBox.getChildren().addAll(cb);
 223             }
 224 
 225 
 226             vBox2.getChildren().add(hBox);
 227         }
 228 
 229         root.getChildren().addAll(vBox1, vBox2);
 230 
 231         stage.setScene(scene);
 232         stage.show();
 233     }
 234 }