21

Good day!
I am developing a program using JavaFX SDK. I wanted to have a message box like in C#:

DialogResult rs = MessageBox.showDialog("Message Here...");
if (rs == ....) {
    // code
}

I want to have a functionality like this using JavaFX SDK. Answers are very much appreciated.

8 Answers 8

24

https://docs.oracle.com/javase/8/javafx/api/javafx/scene/control/Alert.html

The Alert class subclasses the Dialog class, and provides support for a number of pre-built dialog types that can be easily shown to users to prompt for a response.

So the code looks something like

Alert alert = new Alert(AlertType.INFORMATION);
alert.setTitle("Message Here...");
alert.setHeaderText("Look, an Information Dialog");
alert.setContentText("I have a great message for you!");
alert.showAndWait().ifPresent(rs -> {
    if (rs == ButtonType.OK) {
        System.out.println("Pressed OK.");
    }
});
9

Update

As of Java8u40, the core JavaFX libraries include dialog (message box) functionality. Refer to the documentation for the following classes:

For quick info on how to use the Alert class, refer to other answers to this question:

For a longer tutorial, refer to the Makery JavaFX dialog tutorial (this tutorial is highly recommended).

Original Answer

Here is an example of a Modal Confirm dialog. It works by creating a Stage containing a Scene with the dialog contents in it, and then calling show() on the Scene.

If you want the main processing thread to pause whilst the new Stage is shown and you are using JavaFX 2.2+, then you can call showAndWait() on the Stage rather than show. Modified to use show and wait and just display a message and ok button, then processing should act quite similar to a C# MessageBox.

If you want a professional looking message box for Java 8, I recommend using the dialogs from the ControlsFX library, which is a later iteration of the dialogs in the JavaFX UI Controls Sandbox mentioned in blo0p3r's answer.

3
  • I am currently using JavaFX 2.1 and showAndWait() method is not available within a stage class? How can I pause the thread to read the next line of codes when the messagebox is still shown?!
    – iSa
    Jul 26, 2012 at 6:32
  • You cannot directly with the 2.1 public API, you must write the continuation code in a callback EventHandler as shown in the linked sample code.
    – jewelsea
    Jul 26, 2012 at 6:45
  • The gist snippet linked to here seems to be the best option for a dialog box for JavaFX2.x, given licensing issues, professionalism and usability. It's a pity that ControlsFX simply isn't usable commercially as most companies can't risk asking clients/users to install JRE8 until Oracle makes it defacto (still multiple years later since this post). Feb 5, 2014 at 9:41
5

Use the namespace:

import javafx.scene.control.Alert;

Calling from main thread:

public void showAlert() { 
    Alert alert = new Alert(Alert.AlertType.INFORMATION);
    alert.setTitle("Message Here...");
    alert.setHeaderText("Look, an Information Dialog");
    alert.setContentText("I have a great message for you!");
    alert.showAndWait();
}

Calling from not main thread:

public void showAlert() {
    Platform.runLater(new Runnable() {
      public void run() {
          Alert alert = new Alert(Alert.AlertType.INFORMATION);
          alert.setTitle("Message Here...");
          alert.setHeaderText("Look, an Information Dialog");
          alert.setContentText("I have a great message for you!");
          alert.showAndWait();
      }
    });
}
0
4

MessageBox on JavaFX 2.2 by OSS is here

I think it will help you.

MessageBox.show(primaryStage,
    "Message Body",
    "Message Title", 
    MessageBox.ICON_INFORMATION | MessageBox.OK | MessageBox.CANCEL);
3

Here is another simple alternative: https://sites.google.com/site/martinbaeumer/programming/open-source/fxmessagebox

Surprising that there is still no standard message box available in JavaFX 2.2

1

This is what I ended up using, which is part of the JavaFX UI Controls Sandbox as announced here on FX Experience :

This is a nice and easy to use dialog. Can't compare with others as this is the only one I have used. No issues with it.

The code is very concise. Looks like this :

//calling from a different controller and don't have the scene object loaded.
Stage stage = (Stage)deleteButton.getScene().getWindow();
DialogResponse response = Dialogs.showConfirmDialog(stage, "Are you sure ...", "Confirm deletion","Delete?", DialogOptions.OK_CANCEL);
if(response == DialogResponse.OK) {
    //...
}
0

At the moment I use this library for showing Dialogs. Maybe it can be of use for you:

https://github.com/4ntoine/JavaFxDialog

0

This is a very simple example : Alert alert = new Alert(AlertType.CONFIRMATION, "Are you sure you want to proceed?");

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.