Source of AdditionalControlsDemo.java


  1: import javafx.application.Application;
  2: import javafx.scene.Scene;
  3: import javafx.stage.Stage;
  4: import javafx.scene.layout.VBox;
  5: import javafx.scene.control.Button;
  6: import javafx.scene.control.CheckBox;
  7: import javafx.scene.control.RadioButton;
  8: import javafx.scene.control.ToggleGroup;
  9: import javafx.scene.control.Spinner;
 10: import javafx.scene.control.SpinnerValueFactory;
 11: import javafx.scene.control.ChoiceBox;
 12: import javafx.collections.FXCollections;
 13: import javafx.scene.control.Label;
 14: import javafx.event.ActionEvent;
 15: import javafx.event.EventHandler;
 16: 
 17: /**
 18: Simple demonstration of some additional JavaFX
 19: UI controls.
 20: */
 21: public class AdditionalControlsDemo extends Application
 22: {
 23:    public static void main(String[] args)
 24:    {
 25:       launch(args);
 26:    }
 27: 
 28:    @Override
 29:    public void start(Stage primaryStage) throws Exception
 30:    {
 31:           VBox root = new VBox();
 32: 
 33:           // Demonstrate radio buttons
 34:           root.getChildren().add(new Label("Select pizza crust"));
 35:       ToggleGroup toggleCrust = new ToggleGroup();
 36:       RadioButton rbHand = new RadioButton("Hand tossed");
 37:       rbHand.setToggleGroup(toggleCrust);
 38:       rbHand.setSelected(true);
 39:       RadioButton rbDeepDish = new RadioButton("Deep dish");
 40:       rbDeepDish.setToggleGroup(toggleCrust);
 41:           root.getChildren().add(rbHand);
 42:           root.getChildren().add(rbDeepDish);
 43: 
 44:           // Demonstrate checkboxes
 45:           root.getChildren().add(new Label("Select pizza toppings"));
 46:             CheckBox cbCheese = new CheckBox("Extra cheese");
 47:           CheckBox cbPepperoni = new CheckBox("Pepperoni");
 48:           CheckBox cbMushrooms = new CheckBox("Mushrooms");
 49:           root.getChildren().add(cbCheese);
 50:           root.getChildren().add(cbPepperoni);
 51:           root.getChildren().add(cbMushrooms);
 52: 
 53:           // Demonstrate Spinner with integer values from 1-10
 54:           root.getChildren().add(new Label("Select quantity"));
 55:       Spinner<Integer> spinnerQuantity = new Spinner<Integer>();
 56:       final int defaultValue = 1;
 57:       // Value factory.
 58:       SpinnerValueFactory<Integer> quantityFactory =
 59:          new SpinnerValueFactory.IntegerSpinnerValueFactory
 60:                  (1, 10, defaultValue);
 61:       spinnerQuantity.setValueFactory(quantityFactory);
 62:       root.getChildren().add(spinnerQuantity);
 63: 
 64:       // Demonstrate ChoiceBox with delivery options
 65:           root.getChildren().add(new Label("Select delivery mode"));
 66:           ChoiceBox<String> cbModes = new ChoiceBox<String>(
 67:                    FXCollections.observableArrayList("Delivery",
 68:                                    "Dine-In", "Carryout"));
 69:       root.getChildren().add(cbModes);
 70: 
 71:       // Button to display selections
 72:           Button btnSelections = new Button("Get Selections");
 73:           // Set the event handler when the button is clicked
 74:           btnSelections.setOnAction(new EventHandler<ActionEvent>()
 75:           {
 76:                  @Override
 77:                    public void handle(ActionEvent event)
 78:                    {
 79:                            System.out.println("Hand tossed: " + rbHand.isSelected());
 80:                            System.out.println("Deep dish: " +
 81:                        rbDeepDish.isSelected());
 82:                            System.out.println("Cheese: " + cbCheese.isSelected());
 83:                         System.out.println("Pepperoni: " +
 84:                        cbPepperoni.isSelected());
 85:                         System.out.println("Mushrooms: " +
 86:                        cbMushrooms.isSelected());
 87:                         System.out.println("Quantity: " +
 88:                        spinnerQuantity.getValue());
 89:                         System.out.println("Mode: " + cbModes.getValue());
 90:                    }
 91:            }
 92:           );
 93:           root.getChildren().add(btnSelections);
 94: 
 95:            Scene scene = new Scene(root, 350, 300);
 96:       primaryStage.setTitle("Additional Controls Demo");
 97:       primaryStage.setScene(scene);
 98:       primaryStage.show();
 99:    }
100: }