Issue
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;
}
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?
Solution
This should do the trick:
dialog.setOnShown(event -> {
Platform.runLater(textArea::requestFocus);
event.consume();
});
dialog.showAndWait();
Answered By - Miss Chanandler Bong
Answer Checked By - Marilyn (JavaFixing Volunteer)