java 自定义 JavaFX 对话框

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/40031632/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-03 04:52:20  来源:igfitidea点击:

Custom JavaFX Dialog

javajavafxdialog

提问by Dejwi

I want to create a custom JavaFX dialog. So far I've create something like that:

我想创建一个自定义的 JavaFX 对话框。到目前为止,我已经创建了类似的东西:

public class LoginDialog extends Dialog {

    public LoginDialog(Data data) {
        try {
            FXMLLoader loader = new FXMLLoader(getClass().getResource("fxml/LoginDialog.fxml"));
            Parent root = loader.load();
            LoginDialogController controller = loader.<LoginDialogController>getController();
            controller.setModel(new LoginModel(data));
            getDialogPane().setContent(root);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

But - at first I'm not convinced that this is the right way to do this.

但是 - 起初我不相信这是做到这一点的正确方法。

Secondly - I wish to use a Button declared in LoginDialog.fxml file to close this LoginDialog, after showAndWait() call. Is it possible? How can I set the return value?

其次 - 在 showAndWait() 调用之后,我希望使用在 LoginDialog.fxml 文件中声明的 Button 来关闭这个 LoginDialog。是否可以?如何设置返回值?

回答by James_D

Close the window from the controller:

从控制器关闭窗口:

public class LoginDialogController {

    @FXML
    private Button button ;

    @FXML
    private void handleButtonPress() {
        button.getScene().getWindow().hide();
    }

    // ...
}

(and in the FXML you need something like <Button fx:id="button" onAction="#handleButtonPress" ... />)

(在 FXML 中,您需要类似的东西<Button fx:id="button" onAction="#handleButtonPress" ... />

To return a value, specify the type for the dialog and set the result converter:

要返回一个值,请指定对话框的类型并设置结果转换器:

public class LoginDialog extends Dialog<SomeDataType> {

    public LoginDialog(Data data) {
        try {
            FXMLLoader loader = new FXMLLoader(getClass().getResource("fxml/LoginDialog.fxml"));
            Parent root = loader.load();
            LoginDialogController controller = loader.<LoginDialogController>getController();
            controller.setModel(new LoginModel(data));
            getDialogPane().setContent(root);

            setResultConverter(buttonType -> {
                SomeDataType someData = ... ;
                return someData ;
            });

        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}

Obviously you can reference the controller in the result converter, call methods on it, etc, so you can return different values depending on what the user does in the UI.

显然,您可以在结果转换器中引用控制器,对其调用方法等,因此您可以根据用户在 UI 中的操作返回不同的值。

回答by Rahul Singh

You can create a dialog using the following way

您可以使用以下方式创建对话框

public void popup() {
        final Stage dialog = new Stage();
        dialog.setTitle("Confirmation");
        Button yes = new Button("Yes");
        Button no = new Button("No");

        Label displayLabel = new Label("What do you want to do ?");
        displayLabel.setFont(Font.font(null, FontWeight.BOLD, 14));

        dialog.initModality(Modality.NONE);
        dialog.initOwner((Stage) tableview.getScene().getWindow());

        HBox dialogHbox = new HBox(20);
        dialogHbox.setAlignment(Pos.CENTER);

        VBox dialogVbox1 = new VBox(20);
        dialogVbox1.setAlignment(Pos.CENTER_LEFT);

        VBox dialogVbox2 = new VBox(20);
        dialogVbox2.setAlignment(Pos.CENTER_RIGHT);

        dialogHbox.getChildren().add(displayLabel);
        dialogVbox1.getChildren().add(yes);
        dialogVbox2.getChildren().add(no);

        yes.addEventHandler(MouseEvent.MOUSE_CLICKED,
                new EventHandler<MouseEvent>() {
                    @Override
                    public void handle(MouseEvent e) {
                        // inside here you can use the minimize or close the previous stage//
                        dialog.close();
                    }
                });
        no.addEventHandler(MouseEvent.MOUSE_CLICKED,
                new EventHandler<MouseEvent>() {
                    @Override
                    public void handle(MouseEvent e) {
                        dialog.close();
                    }
                });

        dialogHbox.getChildren().addAll(dialogVbox1, dialogVbox2);
        Scene dialogScene = new Scene(dialogHbox, 500, 40);
        dialogScene.getStylesheets().add("//style sheet of your choice");
        dialog.setScene(dialogScene);
        dialog.show();
    }

This method is preferable by me as i donot have to create a sepearate controller and i get a pop up

我更喜欢这种方法,因为我不必创建单独的控制器并且我会弹出