获得从自定义对话框多个结果 - 的JavaFX

问题描述 投票:2回答:1

我有我的JavaFX代码有点问题。我相信大家都知道,你可以用TextInputDialogOptional< String >一个.showAndWait()获得输入。但我应该做的,当我有多个TextFieldsChoiceBox一个自定义对话框?如何从所有的人都点击OK时,你得到的结果吗?我想过一个List<String>,但我没能做到这一点..代码(自定义对话框):

public class ImageEffectInputDialog extends Dialog {

    private ButtonType apply = new ButtonType("Apply", ButtonBar.ButtonData.OK_DONE);
    private ButtonType cancel = new ButtonType("Cancel", ButtonBar.ButtonData.CANCEL_CLOSE);

    public ImageEffectInputDialog(String title) {
        setTitle(title);
        setHeaderText(null);

        GridPane dPane = new GridPane();
        Label offsetX = new Label("Offset X: ");
        Label offsetY = new Label("Offset Y: ");
        Label color = new Label("Shadow Color: ");
        TextField offsetXText = new TextField();
        TextField offsetYText = new TextField();
        ChoiceBox<String> shadowColors = new ChoiceBox<>();
        shadowColors.getItems().add(0, "Black");
        shadowColors.getItems().add(1, "White");
        dPane.setHgap(7D);
        dPane.setVgap(8D);

        GridPane.setConstraints(offsetX, 0, 0);
        GridPane.setConstraints(offsetY, 0, 1);
        GridPane.setConstraints(offsetXText, 1, 0);
        GridPane.setConstraints(offsetYText, 1, 1);
        GridPane.setConstraints(color, 0, 2);
        GridPane.setConstraints(shadowColors, 1, 2);

        dPane.getChildren().addAll(offsetX, offsetY, color, offsetXText, offsetYText, shadowColors);
        getDialogPane().getButtonTypes().addAll(apply, cancel);
        getDialogPane().setContent(dPane);
    }
}

代码(在这里我想要的结果)

if(scrollPane.getContent() != null && scrollPane.getContent() instanceof ImageView) {
    // ImageEffectUtil.addDropShadow((ImageView) scrollPane.getContent());
    ImageEffectInputDialog drop = new ImageEffectInputDialog("Drop Shadow"); 
    //Want the Results here..
}

我希望有人也许能够提供帮助。

java input javafx dialog optional
1个回答
2
投票

首先,为了获得不同类型(通用解决方案)的不同值只是定义了一个新的数据结构,说Result,包含了像OFFSETX,OFFSETY和其他任何你需要的字段。接下来,反而延长的只是Dialog<Result> Dialog。最后,在ImageEffectInputDialog的构造函数,你需要设置的结果转换器,如下所示:

setResultConverter(button -> {
    // here you can also check what button was pressed
    // and return things accordingly
    return new Result(offsetXText.getText(), offsetYText.getText());
});

现在,你需要的地方使用的对话框中,你可以这样做:

    ImageEffectInputDialog dialog = new ImageEffectInputDialog("Title");
    dialog.showAndWait().ifPresent(result -> {
        // do something with result object, which is of type Result
    });
© www.soinside.com 2019 - 2024. All rights reserved.