I have tried the following code, to open a text input dialog box, whenever the textarea gets focused.
TextArea address = new TextArea();
address.focusedProperty().addListener(new ChangeListener<Boolean>() {
@Override
public void changed(ObservableValue<? extends Boolean> arg0, Boolean oldPropertyValue, Boolean newPropertyValue) {
if (newPropertyValue) {
System.out.println("Textfield on focus");
TextInputDialog dialog = new TextInputDialog("walter");
dialog.setTitle("Text Input Dialog");
dialog.setHeaderText("Look, a Text Input Dialog");
dialog.setContentText("Please enter your name:");
// Traditional way to get the response value.
Optional<String> result = dialog.showAndWait();
if (result.isPresent()) {
System.out.println("Your name: " + result.get());
}
} else {
System.out.println("Textfield out focus");
}
}
});
But for every single click on Dialog box a new dialog box is opened.
I just want to open the dialog box once (onFocus of textarea), perform some task and close it. please help me out...!!
I have tried this code and it worked perfectly..!!
When the focus returns from dialog to main stage the textarea will gain a focus again which will be trigger popping up the dialog again. You can focus out from textarea to avoid this:
MCVE: