JavaFX - how to tell if cancel is clicked

How do I know if OK or Cancel is clicked in this JavaFX dialog box.

Dialogue code:

public String delimiter;

public void delimiterYES() throws IOException {
    delimiter=new String();
    TextInputDialog dialog = new TextInputDialog();
    dialog.setTitle("Delimiter");
    dialog.setHeaderText("Enter the delimiter");

    Optional<String> result = dialog.showAndWait();
    if (result.isPresent()) {
        delimiter=result.get();
    }
}

      

+3


source to share


2 answers


If the result is present, the user clicks OK. If there is no result, the user probably clicked Cancel, but they may have just closed the dialog using the OS's close window function.

Optional<String> result = new TextInputDialog().showAndWait();
if (result.isPresent()) {
    // ok was pressed.
} else {
    // cancel might have been pressed.
}

      

To actually know if a button was clicked, you can use a filter as mentioned in the javadoc dialog under Dialog Validation / Interception Actions.



final Button cancel = (Button) dialog.getDialogPane().lookupButton(ButtonType.CANCEL);
cancel.addEventFilter(ActionEvent.ACTION, event ->
    System.out.println("Cancel was definitely pressed")
);

      

Sample code:

import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.stage.Stage;

import java.util.Optional;

public class DialogSample extends Application {
    @Override
    public void start(Stage stage) throws Exception {
        Button showButton = new Button("show");
        showButton.setOnAction(event -> showDialog(stage));
        showButton.setPrefWidth(100);
        stage.setScene(new Scene(showButton));
        stage.show();

        showButton.fire();
    }

    private void showDialog(Stage stage) {
        TextInputDialog dialog = new TextInputDialog();
        dialog.initOwner(stage);
        dialog.setTitle("Delimiter");
        dialog.setHeaderText("Enter the delimiter");

        final Button ok = (Button) dialog.getDialogPane().lookupButton(ButtonType.OK);
        ok.addEventFilter(ActionEvent.ACTION, event ->
            System.out.println("OK was definitely pressed")
        );

        final Button cancel = (Button) dialog.getDialogPane().lookupButton(ButtonType.CANCEL);
        cancel.addEventFilter(ActionEvent.ACTION, event ->
            System.out.println("Cancel was definitely pressed")
        );

        Optional<String> result = dialog.showAndWait();
        if (result.isPresent()) {
            System.out.println("Result present => OK was pressed");
            System.out.println("Result: " + result.get());
        } else {
            System.out.println("Result not present => Cancel might have been pressed");
        }    
    }

    public static void main(String[] args) {
        Application.launch();
    }
}

      

+3


source


Ok I found the answer here JavaFX Dialogs



Result. ListPresent () will return false if the user canceled the dialog.

+2


source







All Articles