JavaFX 2.1 MessageBox

2019-01-26 06:49发布

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条回答
倾城 Initia
2楼-- · 2019-01-26 07:12

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.");
    }
});
查看更多
做个烂人
3楼-- · 2019-01-26 07:13

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) {
    //...
}
查看更多
贼婆χ
4楼-- · 2019-01-26 07:16

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);
查看更多
爱情/是我丢掉的垃圾
5楼-- · 2019-01-26 07:23

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

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

查看更多
看我几分像从前
6楼-- · 2019-01-26 07:26

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();
      }
    });
}
查看更多
Luminary・发光体
7楼-- · 2019-01-26 07:32

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

查看更多
登录 后发表回答