Dialogs and feedback

IA tooling, not an exam topic. These recipes are JavaFX patterns to adapt for your own IA app. Nothing here is examinable, and your IA must be your own work, so change the messages and actions to fit your project.

This page is about talking back to the user: telling them something, asking before you do something risky, and handling input that does not make sense.


Tell the user something

What this does: shows a small pop-up message with an OK button, then waits for the user to close it before the program carries on.

When you would use it in your IA: confirming a save worked, reporting that a search found no results, or telling the user a task finished. Anything where a one-line message is enough.

The pattern:

Imports:

import javafx.scene.control.Alert;
import javafx.scene.control.Alert.AlertType;

Controller method:

@FXML
private void handleSave() {
    saveData();                                  // your own action

    Alert info = new Alert(AlertType.INFORMATION);
    info.setTitle("Saved");
    info.setHeaderText(null);                    // null removes the bold banner line
    info.setContentText("Your changes have been saved.");
    info.showAndWait();
}

Make it your own:

  • Change the title and the setContentText message to match what happened.
  • setHeaderText(null) gives a clean one-line dialog. Pass a short string instead if you want a bold heading above the message.
  • Swap AlertType.INFORMATION for AlertType.WARNING or AlertType.ERROR to change the icon when the message is a caution or a failure.

Watch out for:

  • showAndWait() blocks until the user closes the dialog, which is what you usually want. If you call it from inside a long loop, the user has to click OK every time around, so keep these for single moments, not bulk output.

Mix with: Ask before doing something destructive, Catch and explain bad input, Save and load text.


Ask before doing something that cannot be undone

What this does: shows a confirmation dialog and only runs the risky action if the user clicks OK. Stops accidental clicks from wiping data.

When you would use it in your IA: any button that deletes a record, clears a list, resets a form, or overwrites a file. Wrap the action so the user has to confirm first.

The pattern:

Imports:

import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.scene.control.Alert;
import javafx.scene.control.Alert.AlertType;
import javafx.scene.control.ButtonType;
import java.util.Optional;

Controller method:

@FXML
private void handleDelete(ActionEvent event) {
    Alert confirm = new Alert(AlertType.CONFIRMATION);
    confirm.setTitle("Please confirm");
    confirm.setHeaderText(null);
    confirm.setContentText("Are you sure you want to delete this item?");

    Optional<ButtonType> result = confirm.showAndWait();
    if (result.isPresent() && result.get() == ButtonType.OK) {
        // Only runs if the user clicked OK.
        removeSelectedItem();
    }
    // If the user clicked Cancel, nothing happens.
}

Make it your own:

  • Change the message in setContentText to describe your own action.
  • Replace removeSelectedItem() with whatever your risky action is.
  • Keep the if check exactly as it is. That is the part that makes the guard work.

Watch out for:

  • showAndWait() returns an Optional, which may be empty if the dialog is closed in an unusual way. The isPresent() check handles that, so keep it.
  • Put the real action inside the if block, not after it, or it will run even when the user cancels.

Mix with: Find out which row the user clicked, Tell the user something, Save and load text.


Catch and explain bad input

What this does: tries to read a number from a text field, and if the text is not a number, shows a clear message and sends the cursor back to the field instead of crashing.

When you would use it in your IA: any field where the user types a quantity, age, price, or score that your code then converts with Integer.parseInt or Double.parseDouble. A typed letter or a blank field would otherwise throw an exception.

The pattern:

Imports:

import javafx.fxml.FXML;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;

Controller:

@FXML private TextField txtQuantity;
@FXML private Label lblError;

@FXML
private void handleAdd() {
    try {
        int quantity = Integer.parseInt(txtQuantity.getText().trim());
        lblError.setText("");                    // clear any old error
        addItems(quantity);                      // your own action
    } catch (NumberFormatException e) {
        lblError.setText("Please enter a whole number.");
        txtQuantity.requestFocus();              // put the cursor back so they can fix it
    }
}

Make it your own:

  • Change the error message to name what you expected.
  • Use Double.parseDouble instead for prices or measurements.
  • lblError is an inline Label near the field. You could show an AlertType.ERROR dialog instead if you prefer a pop-up.

Watch out for:

  • Catch NumberFormatException specifically, not a bare Exception. Catching everything hides real bugs.
  • An empty field throws the same exception as a typed letter, so this one catch covers both cases.
  • This is input validation, the same idea you use on forms. Pair it with Allow only numbers in a field to stop bad input before it is ever typed.

Mix with: Allow only numbers in a field, Turn a control on or off, Tell the user something.


© EduCS.me — A resource hub for Computer Science education

This site uses Just the Docs, a documentation theme for Jekyll.