1 /*
   2  * Copyright (c) 2014, 2016, Oracle and/or its affiliates.
   3  * All rights reserved. Use is subject to license terms.
   4  *
   5  * This file is available and licensed under the following license:
   6  *
   7  * Redistribution and use in source and binary forms, with or without
   8  * modification, are permitted provided that the following conditions
   9  * are met:
  10  *
  11  *  - Redistributions of source code must retain the above copyright
  12  *    notice, this list of conditions and the following disclaimer.
  13  *  - Redistributions in binary form must reproduce the above copyright
  14  *    notice, this list of conditions and the following disclaimer in
  15  *    the documentation and/or other materials provided with the distribution.
  16  *  - Neither the name of Oracle Corporation nor the names of its
  17  *    contributors may be used to endorse or promote products derived
  18  *    from this software without specific prior written permission.
  19  *
  20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  21  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  22  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  23  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  24  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  25  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  26  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  27  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  28  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  29  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  30  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  31  */
  32 package ensemble.samples.controls.spinner;
  33 
  34 import javafx.application.Application;
  35 import javafx.collections.FXCollections;
  36 import javafx.collections.ObservableList;
  37 import javafx.scene.Group;
  38 import javafx.scene.Parent;
  39 import javafx.scene.Scene;
  40 import javafx.scene.control.Spinner;
  41 import javafx.scene.control.SpinnerValueFactory;
  42 import javafx.scene.layout.HBox;
  43 import javafx.stage.Stage;
  44 
  45 import java.util.Arrays;
  46 
  47 /**
  48  * A sample that demonstrates the Spinner control.
  49  *
  50  * @sampleName SpinnerApp
  51  * @preview preview.png
  52  * @see javafx.scene.control.Spinner
  53  * @docUrl http://www.oracle.com/pls/topic/lookup?ctx=javase80&id=JFXUI336 Using JavaFX UI Controls
  54  *
  55  * @related /Controls/DialogApp
  56  */
  57 public class SpinnerApp extends Application {
  58 
  59     /**
  60      * Java main for when running without JavaFX launcher
  61      * @param args command line arguments
  62      */
  63     public static void main(String[] args) {
  64         Application.launch(args);
  65     }
  66 
  67     @Override
  68     public void start(Stage primaryStage) throws Exception {
  69         primaryStage.setScene(new Scene(createContent()));
  70         primaryStage.show();
  71     }
  72 
  73     public Parent createContent() {
  74 
  75         HBox intBlock = new HBox(30);
  76         HBox stringBlock = new HBox(30);
  77         HBox doubleBlock = new HBox(30);
  78 
  79         String[] styles = {
  80             "spinner",  // defaults to arrows on right stacked vertically
  81             Spinner.STYLE_CLASS_ARROWS_ON_RIGHT_HORIZONTAL,
  82             Spinner.STYLE_CLASS_ARROWS_ON_LEFT_VERTICAL,
  83             Spinner.STYLE_CLASS_ARROWS_ON_LEFT_HORIZONTAL,
  84             Spinner.STYLE_CLASS_SPLIT_ARROWS_VERTICAL,
  85             Spinner.STYLE_CLASS_SPLIT_ARROWS_HORIZONTAL
  86         };
  87 
  88         Spinner[] intSpinners = new Spinner[styles.length];
  89         Spinner[] stringSpinners = new Spinner[styles.length];
  90         Spinner[] doubleSpinners = new Spinner[styles.length];
  91 
  92         for (int i = 0; i < styles.length; i++) {
  93             /* Integer spinners */
  94             SpinnerValueFactory svf =
  95                 new SpinnerValueFactory.IntegerSpinnerValueFactory(1, 99);
  96             Spinner sp = new Spinner();
  97             sp.setValueFactory(svf);
  98             sp.getStyleClass().add(styles[i]);
  99             sp.setPrefWidth(80);
 100             intSpinners[i] = sp;
 101 
 102             /* Double spinners */
 103             svf = new SpinnerValueFactory.DoubleSpinnerValueFactory(0.0, 1.0,
 104                                                                     0.5, 0.01);
 105             sp = new Spinner();
 106             sp.setValueFactory(svf);
 107             sp.getStyleClass().add(styles[i]);
 108             sp.setPrefWidth(90);
 109             doubleSpinners[i] = sp;
 110 
 111             /* String spinners */
 112             ObservableList<String> items =
 113                 FXCollections.observableArrayList("Grace", "Matt", "Katie");
 114             svf = new SpinnerValueFactory.ListSpinnerValueFactory<>(items);
 115             sp = new Spinner();
 116             sp.setValueFactory(svf);
 117             sp.setPrefWidth(100);
 118             sp.getStyleClass().add(styles[i]);
 119             stringSpinners[i] = sp;
 120         }
 121 
 122         intBlock.getChildren().addAll(Arrays.asList(intSpinners));
 123         doubleBlock.getChildren().addAll(Arrays.asList(doubleSpinners));
 124         stringBlock.getChildren().addAll(Arrays.asList(stringSpinners));
 125 
 126         doubleBlock.setLayoutY(100);
 127         stringBlock.setLayoutY(200);
 128 
 129         Group group = new Group(intBlock, doubleBlock, stringBlock);
 130         // group.getChildren().addAll();
 131         return group;
 132     }
 133 }