Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Introduction to JavaFX Dialogs

HASUNUMA Kenji
November 25, 2014
45

Introduction to JavaFX Dialogs

HASUNUMA Kenji

November 25, 2014
Tweet

Transcript

  1. JavaFX Dialogs (8u40) • Alert - returns any ButtonType •

    TextInputDialog - returns input text • ChoiceDialog - returns choosen item • Dialog - superclass of all dialogs
  2. Dialog on code • Works instead of Stage. • Often,

    exists into a controller as a part of event procedures. • showAndWait() - show and wait, then returns a value as Optional<R>. • show() - show and wait, then returns no value.
  3. // Create a dialog Alert alert = new Alert(INFORMATION); !

    // Setting properties alert.setTitle("Title (INFORMATION)"); alert.setHeaderText("Header Text"); alert.setContentText("Content Text"); ! // Show and obtain the result Optional<ButtonType> result = alert.showAndWait(); ! // Another way if the result is needless // alert.show();
  4. Result types Dialog Result type Alert ButtonType ! OK, CANCEL,

    CLOSE, APPLY, FINISH, YES, NO, NEXT, PREVIOUS TextInputDialog String ChoiceDialog Type of choosen item
  5. /** * Tips #2: the way to resize a dialog.

    */ public class SomeController { @FXML public void onClick(ActionEvent event) { . . . Alert alert = new Alert(INFORMATION); // Using following methods to resize; // setPrefWidth, setPrefHeight and setPrefSize alert.getDialogPane().setPrefSize(400.0, 300.0); Optional<String> result = dialog.showAndWait(); /* Handle the result */ } }
  6. /** * Tips #3: Simple dialog-based application. */ public class

    DialogApp extends Application { @Overrides public void start(Stage stage) throws Exception { TextInputDialog dialog = new TextInputDialog(); /* setting the dialog */ Optional<String> result = dialog.showAndWait(); /* handle the result */ } public static void main(String...args) { launch(args); } }
  7. • The representation of JavaFX dialogs are Dialog and their

    subclasses. • JavaFX Dialog looks like Stage, but is not Stage. • At first create and configure a dialog, and call showAndWait method.