如何在不使用任何“ButtonType”控件的情况下在 JavaFx 中创建自定义对话框?

And*_*ndy 2 java dialog javafx

我想创建一个 custom Dialog,它只显示选项(见图 1)。如果用户选择这些选项之一,对话框应关闭并立即返回相应的结果。

到目前为止,我只能通过向 中添加任意值,通过在单击的选项中使用和应用ButtonTypeDialog隐藏它来完成此操作。 setVisible(false)fire()EventHandler

这种奇怪的解决方法实际上工作正常,但在我看来非常不专业......
那么,如何在不使用 ButtonType 技巧的情况下以更专业或更正确的方式做到这一点?

图1

我的解决方法代码如下所示(Dialog 类):

public class CustomDialog extends Dialog<String> {

    private static final String[] OPTIONS
            = new String[]{"Option1", "Option2", "Option3", "Option4"};
    private String selectedOption = null;
    Button applyButton;

    public CustomDialog() {
        super();
        initStyle(StageStyle.DECORATED);
        VBox vBox = new VBox();
        for (String option : OPTIONS) {
            Button optionButton = new Button(option);
            optionButton.setOnAction((event) -> {
                selectedOption = option;
                applyButton.fire();
            });
            vBox.getChildren().add(optionButton);
        }
        getDialogPane().setContent(vBox);
        getDialogPane().getButtonTypes().add(ButtonType.APPLY);
        applyButton = (Button) getDialogPane().lookupButton(ButtonType.APPLY);
        applyButton.setVisible(false);

        setResultConverter((dialogButton) -> {
            return selectedOption;
        });
    }
}
Run Code Online (Sandbox Code Playgroud)

使用对话框类:

    CustomDialog dialog = new CustomDialog();
    Optional<String> result = dialog.showAndWait();
    String selected = null;
    if (result.isPresent()) {
        selected = result.get();
    } else if (selected == null) {
        System.exit(0);
    }
Run Code Online (Sandbox Code Playgroud)

Jam*_*s_D 7

ADialog只是一个显示DialogPane, 并且引用JavadocsDialogPane的窗口:

DialogPane操作的概念ButtonType。AButtonType是单个按钮的描述符,应在DialogPane. DialogPane因此,创建一个的开发人员必须 指定他们想要显示的按钮类型

(我的重点)。因此,虽然您已经展示了一种可能的解决方法,而在另一个答案中 Slaw 展示了另一种方法,但如果您尝试使用 aDialog而不使用ButtonType它及其关联的结果转换器,那么您实际上是在将该Dialog类用于它不适合的用途.

您描述的功能可以通过常规 modal 完美实现Stage。例如,以下给出了与您描述的相同的基本行为,并且不涉及任何ButtonTypes:

package org.jamesd.examples.dialog;

import java.util.Optional;
import java.util.stream.Stream;

import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.VBox;
import javafx.stage.Modality;
import javafx.stage.Stage;
import javafx.stage.Window;

public class CustomDialog {

    private static final String[] OPTIONS 
        = {"Option 1", "Option 2", "Option 3", "Option 4"};

    private final Stage stage ;

    private String selectedOption = null ;

    public CustomDialog() {
        this(null);
    }

    public CustomDialog(Window parent) {
        var vbox = new VBox();
        // Real app should use an external style sheet:
        vbox.setStyle("-fx-padding: 12px; -fx-spacing: 5px;");
        Stream.of(OPTIONS)
            .map(this::createButton)
            .forEach(vbox.getChildren()::add);
        var scene = new Scene(vbox);
        stage = new Stage();
        stage.initOwner(parent);
        stage.initModality(Modality.WINDOW_MODAL);
        stage.setScene(scene);
    }

    private Button createButton(String text) {
        var button = new Button(text);
        button.setOnAction(e -> {
            selectedOption = text ;
            stage.close();
        });
        return button ;
    }

    public Optional<String> showDialog() {
        selectedOption = null ;
        stage.showAndWait();
        return Optional.ofNullable(selectedOption);
    }
}
Run Code Online (Sandbox Code Playgroud)

这是一个使用此自定义对话框的简单应用程序类:

package org.jamesd.examples.dialog;
import javafx.application.Application;
import javafx.application.Platform;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;

public class App extends Application {

    @Override
    public void start(Stage stage) throws Exception {
        var root = new VBox();
        // Real app should use an external style sheet:
        root.setStyle("-fx-padding: 12px; -fx-spacing: 5px;");
        var showDialog = new Button("Show dialog");
        var label = new Label("No option chosen");
        showDialog.setOnAction(e -> 
            new CustomDialog(stage)
                .showDialog()
                .ifPresentOrElse(label::setText, Platform::exit));
        root.getChildren().addAll(showDialog, label);
        stage.setScene(new Scene(root));
        stage.show();
    }

}
Run Code Online (Sandbox Code Playgroud)