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 static javafx.scene.paint.Color.GHOSTWHITE;
  29 import javafx.application.Application;
  30 import javafx.collections.ObservableList;
  31 import javafx.scene.Group;
  32 import javafx.scene.Node;
  33 import javafx.scene.Scene;
  34 import javafx.scene.control.ToggleButton;
  35 import javafx.scene.control.ToggleGroup;
  36 import javafx.stage.Stage;
  37 
  38 /**
  39  * Demo for {@code ToggleButton}.
  40  */
  41 public class HelloToggleButton extends Application {
  42 
  43     public static void main(String[] args) {
  44         Application.launch(args);
  45     }
  46 
  47     @Override public void start(Stage stage) {
  48         Scene scene = newScene();
  49         
  50         final ToggleGroup group = new ToggleGroup();
  51         group.selectedToggleProperty().addListener(ov -> System.out.println("UserData for selected Toggle: " +
  52                 (group.getSelectedToggle() == null ? "****** no selected toggle******" :
  53                     group.getSelectedToggle().getUserData())));
  54         
  55         ToggleButton button1 = new ToggleButton("No, *I* am your father");
  56         button1.setUserData("Button 1");
  57         button1.setToggleGroup(group);
  58         button1.setSelected(true);
  59 
  60         ToggleButton button2 = new ToggleButton("Nooooooooo!");
  61         button2.setUserData("Button 2");
  62         button2.setLayoutY(40);
  63         button2.setToggleGroup(group);
  64         
  65 
  66         ObservableList<Node> content = ((Group)scene.getRoot()).getChildren();
  67         content.add(button1);
  68         content.add(button2);
  69 
  70         stage.setScene(scene);
  71         stage.show();
  72     }
  73 
  74     private static Stage newStage() {
  75         Stage stage = new Stage();
  76         stage.setTitle("Hello ToggleButton");
  77         stage.setWidth(600);
  78         stage.setHeight(450);
  79         return stage;
  80     }
  81     
  82     private static Scene newScene() {
  83         Scene scene = new Scene(new Group());
  84         scene.setFill(GHOSTWHITE);
  85         return scene;
  86     }
  87 }