Search code examples
javajavafxmessagebox

JavaFX set MessageBox parameters


I have a JavaFX program, it shows a simple MessageBox.

Box class:

public class Box extends Application implements Runnable{



    private DialogFX dialog1 = null;

    private String message = null;
    private String type = null;
    private String title = null;

    public Box(){}

    public void ShowBox(String ptype, String ptitle, String pmessage){

        type = ptype;
        title = ptitle;
        message = pmessage;


    }

    public void run() {
        Application.launch();
    }

    public void start(Stage stage){

         dialog1 = new DialogFX();
         dialog1.setTitleText(title);
         dialog1.setMessage(message);
         dialog1.showDialog();


    }
}

This class is using DialogFX library to create and show a simple message box. In the class there is a function called ShowBox which gets three parameters. These parameters is used to initialize the message box (title, type and message). In the start() method the new DialogFX object is created with needed parameters.

My problem is that when I create a new object of the Box class and set the three string parameter they are stay null in the start() method and the empty dialog box appears.

Example class to create a Box object:

public class MessageFrame {


    private static Box b = null;

    public static void main(String[] args) {
        b = new Box();
        b.ShowBox("ACCEPT", "Title", "Message here");
        b.run();

    }
}

I hope you can help me. What can be the problem?


Solution

  • I am not a JavaFX expert, but the problem is that actually two Box instances are created, one by you with:

    b = new Box();
    

    and one by the JavaFX framework when you call

    Application.launch();
    

    within your run() method. This latter instance will be your application (and the first created by you is not used at all) and it is not initialized properly (ShowBox(...) was not called on it).