return values from javafx dialog


Question

I created a form with javafx that opens a login dialog, after the user enters the login information correctly the dialog closed and the main form loaded some data, what I need is that when the login dialog is closed it would return the user id (which make the login) to the main form, the code of the above case is like that:

The main form

 Stage loginDialog = new LoginDialog(stage);
 loginDialog.sizeToScene();
 loginDialog.showAndWait();

the login dialog form

/* do the login */
close();
/* need to return thew user id to the main form*/

Any help please

1
2
5/29/2013 9:36:09 AM

Accepted Answer

The first thing I would suggest is to create your own simple dialog box. The controlsFX stuff is cool, but my experience has been that some of the controls are overly complex and others have bugs. Here is an abridged example.

public class DialogBox {
    private static String[] login;

    public static String[] display(String title, String message) {
        Stage window = new Stage();
        window.initModality(Modality.APPLICATION_MODAL);
        window.setTitle(title);
        window.setWidth(300);
        window.setHeight(175);
        window.initStyle(StageStyle.UTILITY);
        Label label = new Label(message);

        // Set up the JavaFX button controls and listeners and the text fields
        // for the login info. The button listeners set the login values

        window.setScene(new Scene(root, 300, 175);
        window.showAndWait();
        return login;
    }
} 

As you can see, there is a single static method called display() that returns a String array that contains the user login information. Simply do a static call to this method as follows.

String[] login = DialogBox.display("Login Dialog", "Enter User Name and Password");
2
7/21/2017 9:00:19 PM

Your answer is ControlsFX library. I have posted here.

This library allows you to get return value from a dialog box. The best of all, you can create your own customized dialog box.


Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow
Icon