Home > Articles > Programming > Java

  • Print
  • + Share This
From the author of

Alerts

Finally, a new class, javafx.stage.Alert, has been added to support showing alert dialogs. We've already seen the Alert information type dialog in previous examples. In addition to information dialogs, Alert supports question and confirmation dialogs.

To create an information alert, use the inform static function from the Alert class:

Alert.inform("Information", "This is an information alert");

This example produces the dialog shown in Figure 19.

The question and confirmation alerts are similar; however, they contain buttons for the user to answer the question or confirm an action. Both of these alert functions return a Boolean value indicating the result of the user action. You create a question alert by using the question static function from the Alert class:

var result = Alert.question("Question Alert", "Do you like JavaFX?"); 

This example creates the display shown in Figure 20.

The variable, result, will be true if the user selected the Yes button and false if the user selected No. (Of course, in this particular case, the answer will always be Yes!!)

The confirmation alert is similar to the question alert. However, now the buttons read Cancel and OK (see Figure 21):

var result = Alert.confirm("Confirm Alert", "Are you ready to exit?");

if(result) {
    FX.exit();
}
  • + Share This
  • 🔖 Save To Your Account