从javafx对话框返回值

时间:2013-05-29 09:36:09

标签: javafx

我用javafx创建了一个打开登录对话框的表单,在用户正确输入登录信息后关闭对话框并且主表单加载了一些数据,我需要的是当登录对话框关闭时它将返回用户id(进行登录)到主窗体,上面例子的代码是这样的:

主要表格

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

登录对话框表单

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

请帮助

2 个答案:

答案 0 :(得分:2)

我建议的第一件事是创建自己的简单对话框。 controlsFX的东西很酷,但我的经验是,一些控件过于复杂,而另一些则有错误。这是一个简略的例子。

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;
    }
} 

正如您所看到的,有一个名为display()的静态方法,它返回一个包含用户登录信息的String数组。只需对此方法进行静态调用,如下所示。

String[] login = DialogBox.display("Login Dialog", "Enter User Name and Password");

答案 1 :(得分:0)

您的答案是ControlsFX库。 我已发布here

此库允许您从对话框中获取返回值。最重要的是,您可以创建自己的自定义对话框。

相关问题