APD Week 5

Segment 3 - Standard Dialog Box

ยท

3 min read

Standard Dialog Box

A Dialog Box is a secondary window (i.e., not the main application window) that is used to interact with the user ... usually to display or obtain additional information.

Dialog Boxes

Alert

Example 1 :

Alert alert = new Alert(Alert.AlertType.INFORMATION);
alert.showAndWait();

Output:

methods:

  1. setTitle()

  2. setHeaderText()

    setHeaderText(null) // to remove

  3. setContainerText() //

  4. getDialogPane(): The dialog pane is the main content area of the dialog where you can customize its appearance and content.

  5. getDialogPane().setExpandableContent(expandableContent) : expandable content is additional information or details that the user can choose to show or hide by clicking on an expand/collapse button or similar control.

  6. alert.getButtonTypes().setAll(); : Erase all the default buttons

  7. alert.getButtonTypes().add(); : Add the buttons

    ๐Ÿ’ก
    There is a limit on how many buttons would fit nicely on the window
  8. getDialogPane().setContent(); : too add the content to dialog layout

Example 2:

Alert alert = new Alert(Alert.AlertType.WARNING);
alert.setTitle("Tip for the day");
alert.setHeaderText(null);
alert.setContentText("Don't eat yellow snow.");

Output:

Example 3:

Alert alert = new Alert(Alert.AlertType.ERROR);
alert.setTitle("Error"); alert.setHeaderText(null); 
alert.setContentText("Invalid Gregorian Calendar Date");
Label label = new Label("Date entered:");
TextArea textArea = new TextArea(new GregorianCalendar().toString()); 
textArea.setEditable(false); textArea.setWrapText(true); 
textArea.setMaxWidth(Double.MAX_VALUE); 
textArea.setMaxHeight(Double.MAX_VALUE);
GridPane.setVgrow(textArea, Priority.ALWAYS);
GridPane.setHgrow(textArea, Priority.ALWAYS);
GridPane expandableContent = new GridPane(); 
expandableContent.setMaxWidth(Double.MAX_VALUE); expandableContent.add(label, 0, 0);
expandableContent.add(textArea, 0, 1); 
alert.getDialogPane().setExpandableContent(expandableContent);

Output:

To store the result from the dialog box:
Optional<ButtonType> result = alert.showAndWait();

The get() method to get the value that was selected:

result.get()

Alert alert = new Alert(Alert.AlertType.CONFIRMATION);
alert.setTitle("Answer this Question");
alert.setHeaderText(null);
alert.setContentText("Do you want me to clean up your hard drive ?");
Optional<ButtonType> result = alert.showAndWait();
if (result.get() == ButtonType.OK){
System.out.println("OK, I'm erasing it now ..."); }
else { System.out.println("Fine then, you clean it up!"); }

ChoiceDialog

Optional<ButtonType> result = alert.showAndWait();

result.isPresent() :The isPresent() method allows us to determine whether or not the user selected an item or pressed CANCEL.

Example 1:

String[] options = {"Apple", "Orange", "Strawberry", "Banana", "Peaches"};
ChoiceDialog<String> dialog = new ChoiceDialog<String>("Peaches", options); // sets default to Peaches
dialog.setTitle("Fruit Information"); dialog.setHeaderText(null);
dialog.setContentText("Choose your favorite fruit");
Optional<String> result = dialog.showAndWait();
if (result.isPresent()){
System.out.println("Your choice: " + result.get()); }

Output:

TextInputDialogBox

Example 1:

TextInputDialog dialog = new TextInputDialog("Mark");
dialog.setTitle("Input Required"); dialog.setHeaderText(null); dialog.setContentText("Please enter your name:");
Optional<String> result = dialog.showAndWait();
if (result.isPresent()){
System.out.println("Your name is: " + result.get()); }

Output:

Dialog

Example 1:

Dialog dialog = new Dialog();
dialog.setTitle("Login Dialog");
dialog.setHeaderText(null);
ButtonTypeloginButtonType= new ButtonType("Login", ButtonBar.ButtonData.OK_DONE); 
dialog.getDialogPane().getButtonTypes().addAll(loginButtonType, ButtonType.CANCEL);
Optional result = dialog.showAndWait();
if (result.isPresent()) {
System.out.println("Username = " + username.getText() + ", Password = " + password.getText());

}

Output:

๐Ÿ’ก
To get the result we used the Optional result we didn't specify the type.

In this way we cannot access the data of username and password directly if we place it somwhere else

To do that We can replace the above code with this code:

// Convert the result to a Pair containing the username and password dialog.setResultConverter( new Callback<ButtonType, Pair<String, String>>()
{
public Pair<String, String> call(ButtonType b) {
if (b == loginButtonType) { return new Pair<String,String>(username.getText(), password.getText());
}
return null;
}
});
Optional<Pair<String, String>> result = dialog.showAndWait();
if (result.isPresent()) System.out.println("Username = " + result.get().getKey() + ", Password = " + result.get().getValue());
ย