1 /*
   2  * Copyright (c) 2010, 2013, 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.beans.binding.Bindings;
  30 import javafx.beans.binding.StringExpression;
  31 import javafx.css.PseudoClass;
  32 import javafx.geometry.Insets;
  33 import javafx.geometry.Pos;
  34 import javafx.scene.Scene;
  35 import javafx.scene.control.CheckBox;
  36 import javafx.scene.control.Label;
  37 import javafx.scene.control.Separator;
  38 import javafx.scene.layout.VBox;
  39 import javafx.scene.paint.Color;
  40 import javafx.stage.Stage;
  41 
  42 public class HelloCheckBox extends Application {
  43     
  44     private CheckBox createSteadyStateControl(String text) {
  45         
  46         CheckBox checkBox = new CheckBox(text);
  47         for(String pseudoClass : text.split(" ")) {
  48             checkBox.pseudoClassStateChanged(PseudoClass.getPseudoClass(pseudoClass), true);
  49         }
  50         checkBox.setFocusTraversable(false);
  51         checkBox.setMouseTransparent(true);
  52         return checkBox;
  53     }
  54     
  55     @Override public void start(Stage stage) {
  56 
  57         CheckBox[] steadyStateControls = new CheckBox[] {
  58             createSteadyStateControl("selected focused hover"),
  59             createSteadyStateControl("selected focused"),
  60             createSteadyStateControl("selected hover"),
  61             createSteadyStateControl("indeterminate focused hover"),
  62             createSteadyStateControl("indeterminate focused"),
  63             createSteadyStateControl("indeterminate hover"),
  64             createSteadyStateControl("hover focused"),
  65             createSteadyStateControl("focused"),
  66             createSteadyStateControl("hover")
  67         };
  68         
  69         VBox steadyState = new VBox(7);
  70         steadyState.setTranslateX(20);
  71         steadyState.getChildren().add(new Label("Steady pseudo-class state samples")); 
  72         steadyState.getChildren().addAll(steadyStateControls);
  73         
  74         CheckBox cbox = new CheckBox("Indeterminate CheckBox");
  75         cbox.setIndeterminate(true);
  76         cbox.setAllowIndeterminate(true);
  77 
  78         Label label = new Label();
  79         label.textProperty().bind(
  80                 Bindings.when(cbox.indeterminateProperty()).
  81                         then("The check box is indeterminate").
  82                         otherwise(
  83                                 Bindings.when(cbox.selectedProperty()).
  84                                         then("The check box is selected").
  85                                         otherwise("The check box is not selected"))
  86         );
  87 
  88         VBox vbox = new VBox(7);
  89         vbox.setAlignment(Pos.CENTER);
  90         vbox.getChildren().addAll(label, cbox);
  91         
  92         CheckBox twoStateCheckBox = new CheckBox("Two-state check box");
  93         twoStateCheckBox.setAllowIndeterminate(false);
  94         twoStateCheckBox.setIndeterminate(false);
  95         twoStateCheckBox.setSelected(true);
  96         
  97         Label twoStateLabel = new Label();
  98         twoStateLabel.textProperty().bind(Bindings.when(twoStateCheckBox.selectedProperty()).
  99                                              then("Selected"). otherwise("Not selected"));
 100         
 101         VBox vbox2 = new VBox(7);
 102         vbox2.setAlignment(Pos.CENTER);
 103         vbox2.getChildren().addAll(twoStateLabel, twoStateCheckBox);
 104 
 105         CheckBox focusCheckBox = new CheckBox("Focus indicator");
 106         focusCheckBox.setAllowIndeterminate(false);
 107         focusCheckBox.setIndeterminate(false);
 108         
 109         Label focusLabel = new Label();
 110         StringExpression s = Bindings.concat(Bindings.when(focusCheckBox.focusedProperty()).
 111                                                 then("Focused"). otherwise("Not focused"),
 112                                              " and ",
 113                                              Bindings.when(focusCheckBox.selectedProperty()).
 114                                                 then("Selected").otherwise("Not selected")
 115                                              );
 116         focusLabel.textProperty().bind(s);
 117 
 118         VBox vbox3 = new VBox(7);
 119         vbox3.setAlignment(Pos.CENTER);
 120         vbox3.getChildren().addAll(focusLabel, focusCheckBox);
 121 
 122         VBox mainbox = new VBox(7);
 123         mainbox.setAlignment(Pos.CENTER);
 124         mainbox.getChildren().addAll(vbox, new Separator(),
 125                                      vbox2, new Separator(),
 126                                      vbox3, new Separator(),
 127                                      steadyState);
 128         
 129         Scene scene = new Scene(mainbox, 400, 450);
 130         scene.setFill(Color.SKYBLUE);
 131         stage.setTitle("Hello CheckBox");
 132         stage.setScene(scene);
 133         stage.show();
 134     }
 135 
 136     public static void main(String[] args) {
 137         Application.launch(args);
 138     }
 139 }