Dialogs

Steps

  1. Create a Dialog object (new Dialog())
  2. Set the title and header text
  3. Create a pane and put in some contents
  4. Create result types for each button
  5. Set the content for the dialog (dialog.getDialogPane().setContent(pane))
  6. Add each button type (dialog.getDialogPane().getButtonTypes().add(type))
  7. Create a result converter (use a lambda, probably)
  8. Call showAndWait for the dialog
  9. showAndWait returns a ResultType value

Dialogs.java

// Dialogs.java
//    Demonstrates construction of dialog boxes.

import java.util.*;
import javafx.application.*;
import javafx.geometry.*;
import javafx.scene.*;
import javafx.scene.control.*;
import javafx.scene.layout.*;
import javafx.stage.*;

public class Dialogs extends Application {
  public Dialog<String> dialog;

  public void createDialog() {
    dialog = new Dialog<String>();
    dialog.setTitle("Dialog");
    dialog.setHeaderText("Fill in the blank");
    dialog.setResizable(true);

    // Create some labels.
    Label cityLabel = new Label("City");
    Label stateLabel = new Label("State");
    Label zipLabel = new Label("Zip Code");

    // Create some input fields.
    TextField city = new TextField();
    TextField state = new TextField();
    TextField zip = new TextField();

    // Create a grid to hold everything.
    GridPane grid = new GridPane();
    grid.setHgap(20);
    grid.setVgap(20);
    grid.setPadding(new Insets(20, 20, 20, 20));
    grid.add(cityLabel, 0, 0);
    grid.add(stateLabel, 0, 1);
    grid.add(zipLabel, 0, 2);
    grid.add(city, 1, 0);
    grid.add(state, 1, 1);
    grid.add(zip, 1, 2);

    // Create buttons types for the return values.
    ButtonType okType =
      new ButtonType("OK", ButtonBar.ButtonData.OK_DONE);
    ButtonType cancelType =
      new ButtonType("Cancel", ButtonBar.ButtonData.CANCEL_CLOSE);

    // Add the grid to the dialog.
    dialog.getDialogPane().setContent(grid);
    dialog.getDialogPane().getButtonTypes().add(okType);
    dialog.getDialogPane().getButtonTypes().add(cancelType);

    // Create the result converter.
    dialog.setResultConverter(
      b -> b == okType ?
                (city.getText() + ", " + state.getText() + " " + zip.getText()) :
                null);
  }

  public void showDialog() {
    Optional<String> result = dialog.showAndWait();
    if (result.isPresent()) System.out.println(result.get());
    else System.out.println("cancelled");
  }

  public void start(Stage stage) {
    // Create a button to show the dialog.
    Button button = new Button("Show Dialog");
    
    button.setOnAction(e -> showDialog());
    
    // Create the dialog
    createDialog();

    // Create a pane to hold the label.
    BorderPane pane = new BorderPane();
    pane.setCenter(button);

    // Create a scene.
    Scene scene = new Scene(pane, 600, 600);

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

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