JavaFX 2.1 MessageBox

后端 未结 8 1490
没有蜡笔的小新
没有蜡笔的小新 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:27

    Update

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

    • Alert
    • Dialog (and subclasses)

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

    • by user Limited Atonemment
    • by user Andrei Krasutski

    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.

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

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

    • Link to tutorial : http://code.makery.ch/blog/javafx-2-dialogs
    • Link to source code : https://github.com/marcojakob/javafx-ui-sandbox/tree/master/javafx-dialogs

    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 讨论(0)
提交回复
热议问题