xkevio
xkevio

Reputation: 540

Get Multiple Results from Custom Dialog -- JavaFX

I've got a little problem with my JavaFX code. I'm sure you all know that you can get the input from a TextInputDialog with an Optional< String > and .showAndWait(). But what should I do when I have a custom dialog with multiple TextFields and a ChoiceBox? How do I get the results from all of them when clicking OK? I thought about a List<String> but I didn't manage to do it.. Code (Custom Dialog):

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

Code (where I want the results)

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

I hope someone might be able to help.

Upvotes: 4

Views: 5617

Answers (1)

AlmasB
AlmasB

Reputation: 3407

First of all, in order to obtain different values of different types (generic solution) just define a new data structure, say Result, which contains fields like offsetX, offsetY and whatever else you need. Next, extend Dialog<Result> instead of just Dialog. Finally, in the constructor of your ImageEffectInputDialog you need to set result converter, as follows:

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

Now wherever you need to use the dialog, you can do:

    ImageEffectInputDialog dialog = new ImageEffectInputDialog("Title");
    dialog.showAndWait().ifPresent(result -> {
        // do something with result object, which is of type Result
    });

Upvotes: 5

Related Questions