JavaFX 2.1 MessageBox

后端 未结 8 1489
没有蜡笔的小新
没有蜡笔的小新 2020-12-11 01:48

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

DialogResult rs = MessageBox.showDialog(\"Mess         


        
相关标签:
8条回答
  • 2020-12-11 02:07

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

    0 讨论(0)
  • 2020-12-11 02:08

    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

    0 讨论(0)
  • 2020-12-11 02:10

    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 讨论(0)
  • 2020-12-11 02: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.");
        }
    });
    
    0 讨论(0)
  • 2020-12-11 02:15

    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);
    
    0 讨论(0)
  • 2020-12-11 02:17

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

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

    0 讨论(0)
提交回复
热议问题