JavaFX Controls

Button-like Things

  • Buttons
  • CheckBoxes
  • RadioButtons
  • ToggleButtons
  • ChoiceBoxes

Buttons

  • Used to initiate an action (e.g. close a dialog)
  • Define onAction

Buttons.java

// Buttons.java
//     Demonstrates the use of buttons.

import javafx.application.*;
import javafx.event.*;
import javafx.geometry.*;
import javafx.scene.*;
import javafx.scene.control.*;
import javafx.scene.layout.*;
import javafx.stage.*;

public class Buttons extends Application {

  public void start(Stage stage) {
    // Create a dialog box.
    Alert alert = new Alert(Alert.AlertType.INFORMATION, "The button was pressed.");
    alert.setTitle("Information Dialog");
    alert.setHeaderText("Information Dialog");
    
    // Create two buttons.
    Button button1 = new Button("Press Me");
    button1.setOnAction(e -> alert.showAndWait());
    Button button2 = new Button("Quit");
    button2.setOnAction(e -> Platform.exit());

    // Create a box to hold the buttons.
    HBox box = new HBox(20);
    box.setPadding(new Insets(20, 20, 20, 20));
    box.getChildren().addAll(button1, button2);

    // Create a scene.
    Scene scene = new Scene(box);

    // Set up the stage and make it visible.
    stage.setTitle("Buttons");
    stage.setScene(scene);
    stage.show();
  }

  public static void main(String[] args) {
    launch(args);
  }
}

CheckBoxes

  • Used to turn on or off one or more independent choices
  • Changing a checkbox should not cause an immediate action
  • Typical use
    • Change checkbox values in a dialog
    • The action occurs when the dialog is accepted.

CheckBoxes.java

// CheckBoxes.java
//     Demonstrates the use of checkboxes.

import javafx.application.*;
import javafx.event.*;
import javafx.geometry.*;
import javafx.scene.*;
import javafx.scene.control.*;
import javafx.scene.layout.*;
import javafx.stage.*;

public class CheckBoxes extends Application {
  // These variables work around a known bug where
  // the alert box doesn't properly resize after the
  // text is changed.
  boolean gotAlertHeight = false;
  double alertHeight;
  
  public void start(Stage stage) {
    // Create a dialog box.
    Alert alert = new Alert(Alert.AlertType.INFORMATION, "");
    alert.setTitle("CheckBox Status");
    alert.setHeaderText("CheckBox Status");
    alert.setResizable(true);
    
    // Create some checkboxes.
    CheckBox box1 = new CheckBox("Box 1");
    CheckBox box2 = new CheckBox("Box 2");
    CheckBox box3 = new CheckBox("Box 3");
                    
    // Create a button.
    Button button = new Button("Examine");
    button.setOnAction(e -> {
        String message =
            "Box 1 is " + (box1.isSelected() ? "" : "not") + " selected\n" +
            "Box 2 is " + (box2.isSelected() ? "" : "not") + " selected\n" +
            "Box 3 is " + (box3.isSelected() ? "" : "not") + " selected";
        alert.setContentText(message);
        // Set the remembered height of the alert box.
        if (gotAlertHeight) {
          alert.setHeight(alertHeight);
        }
        alert.showAndWait();
        // Remember the height of the alert box.
        if (!gotAlertHeight) {
          alertHeight = alert.getHeight();
          gotAlertHeight = true;
        }
      });
          

    // Create a box to hold the buttons.
    HBox box = new HBox(20);
    box.setPadding(new Insets(20, 20, 20, 20));
    box.getChildren().addAll(box1, box2, box3, button);

    // Create a scene.
    Scene scene = new Scene(box);

    // Set up the stage and make it visible.
    stage.setTitle("CheckBoxes");
    stage.setScene(scene);
    stage.show();
  }

  public static void main(String[] args) {
    launch(args);
  }
}

Radio Buttons

  • Used to turn on or off multiple choices when only one choice can be selected at any given time
  • Changing a radio button should not cause an immediate action
  • Typical use
    • Change radio button values in a dialog
    • The action occurs when the dialog is accepted.

RadioButtons.java

// RadioButtons.java
//     Demonstrates the use of checkboxes.

import javafx.application.*;
import javafx.event.*;
import javafx.geometry.*;
import javafx.scene.*;
import javafx.scene.control.*;
import javafx.scene.layout.*;
import javafx.stage.*;

public class RadioButtons extends Application {
  RadioButton button1;
  RadioButton button2;
  RadioButton button3;
  
  void showDialog() {
    // Create the dialog box.
    String message =
      "Button 1 is " + (button1.isSelected() ? "" : "not") + " selected\n" +
      "Button 2 is " + (button2.isSelected() ? "" : "not") + " selected\n" +
      "Button 3 is " + (button3.isSelected() ? "" : "not") + " selected";
    Alert alert = new Alert(Alert.AlertType.INFORMATION, message);
    alert.setTitle("Radio Button Status");
    alert.setHeaderText("Radio Button Status");
    alert.setResizable(true);
    alert.showAndWait();
  }
  
  public void start(Stage stage) {
    // Create a group of radio buttons.
    ToggleGroup group = new ToggleGroup();
    button1 = new RadioButton("Button 1");
    button2 = new RadioButton("Button 2");
    button3 = new RadioButton("Button 3");
    button1.setSelected(true);
    button1.setToggleGroup(group);
    button2.setToggleGroup(group);
    button3.setToggleGroup(group);
                    
    // Create a button.
    Button button = new Button("Examine");
    button.setOnAction(e -> showDialog());

    // Create a box to hold the buttons.
    HBox box = new HBox(20);
    box.setPadding(new Insets(20, 20, 20, 20));
    box.getChildren().addAll(button1, button2, button3, button);

    // Create a scene.
    Scene scene = new Scene(box);

    // Set up the stage and make it visible.
    stage.setTitle("RadioButtons");
    stage.setScene(scene);
    stage.show();
  }

  public static void main(String[] args) {
    launch(args);
  }
}

Toggle Buttons

  • Used to turn a single choice on or off
  • Should cause an immediate action

ToggleButtons.java

// ToggleButtons.java
//     Demonstrates the use of checkboxes.

import javafx.application.*;
import javafx.event.*;
import javafx.geometry.*;
import javafx.scene.*;
import javafx.scene.control.*;
import javafx.scene.layout.*;
import javafx.stage.*;

public class ToggleButtons extends Application {
  ToggleButton button;
  
  void toggle() {
    // Change the button text to reflect the status.
    String status = button.isSelected() ? "on" : "off";
    button.setText(status);

    // Create the dialog box.
    Alert alert = new Alert(Alert.AlertType.INFORMATION,
                            "Toggle is " + status);
    alert.setTitle("Toggle Button Status");
    alert.setHeaderText("Toggle Button Status");
    alert.setResizable(true);

    // Make it visible.
    alert.showAndWait();
  }
  
  public void start(Stage stage) {
    
    // Create a group of radio buttons.
    button = new ToggleButton("Off");
    button.setSelected(false);
    button.setOnAction(e -> toggle());
                    
    // Create a box to hold the buttons.
    HBox box = new HBox(20);
    box.setPadding(new Insets(20, 20, 20, 20));
    box.getChildren().addAll(button);

    // Create a scene.
    Scene scene = new Scene(box);

    // Set up the stage and make it visible.
    stage.setTitle("ToggleButtons");
    stage.setScene(scene);
    stage.show();
  }

  public static void main(String[] args) {
    launch(args);
  }
}

Choice Boxes

  • Used to make a choice between several options.
  • Choice Boxes vs Radio Buttons
    • With radio buttons you can see all the choices
    • Choice boxes take less space
    • Use choice boxes for more than 5? options

ChoiceBoxes.java

// ChoiceBoxes.java
//     Demonstrates the use of choice boxes.

import javafx.application.*;
import javafx.collections.*;
import javafx.event.*;
import javafx.geometry.*;
import javafx.scene.*;
import javafx.scene.control.*;
import javafx.scene.layout.*;
import javafx.stage.*;

public class ChoiceBoxes extends Application {
  ChoiceBox<String> choice;
  
  void showDialog() {

    // Create the dialog box.
    Alert alert = new Alert(Alert.AlertType.INFORMATION,
                            "The choice is " + choice.getValue() + ".");
    alert.setTitle("Choice Box Status");
    alert.setHeaderText("Choice Box Status");
    alert.setResizable(true);

    // Make it visible.
    alert.showAndWait();
  }
  
  public void start(Stage stage) {
    
    // Create a choice box.
    choice =
      new ChoiceBox<String>(FXCollections.observableArrayList("Choice 1",
                                                              "Choice 2",
                                                              "Choice 3"));
    choice.setValue("Choice 1");
    choice.setOnAction(e -> showDialog());
                    
    // Create a box to hold the buttons.
    HBox box = new HBox(20);
    box.setPadding(new Insets(20, 20, 20, 20));
    box.getChildren().addAll(choice);

    // Create a scene.
    Scene scene = new Scene(box);

    // Set up the stage and make it visible.
    stage.setTitle("ChoiceBoxes");
    stage.setScene(scene);
    stage.show();
  }

  public static void main(String[] args) {
    launch(args);
  }
}