3
public Node dialog(){
        Pane root = new Pane();
        root.setPrefWidth(200);
        root.setPrefHeight(200);
        Button button = new Button("Dialog");
        button.setOnAction(event -> {
            Dialog dialog = new Dialog();
            TextArea textArea = new TextArea();
            dialog.getDialogPane().setContent(textArea);
            ButtonType ok = new ButtonType("OK", ButtonBar.ButtonData.OK_DONE);
            ButtonType cancel = new ButtonType("Cancel", ButtonBar.ButtonData.CANCEL_CLOSE);
            DialogPane dialogPane = dialog.getDialogPane();
            dialogPane.getButtonTypes().addAll(ok, cancel);
            textArea.requestFocus();
            dialog.showAndWait();
        });
        root.getChildren().add(button);
        return root;
    }

enter image description here

I try use textArea.requestFocus(); before dialog.showAndWait(); but when dialog open it always
focus OK Button. How to foucs textArea when dialog first open?

2

1 Answer 1

3

This should do the trick:

dialog.setOnShown(event -> {
    Platform.runLater(textArea::requestFocus);
    event.consume();
});
dialog.showAndWait();
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

Draft saved
Draft discarded

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.