在Javafx中,当文本区域获得焦点时,如何打开文本输入对话框?
我尝试了以下代码,在文本区域聚焦时打开文本输入对话框在Javafx中,当文本区域获得焦点时,如何打开文本输入对话框?,javafx,dialog,textarea,Javafx,Dialog,Textarea,我尝试了以下代码,在文本区域聚焦时打开文本输入对话框 TextArea address = new TextArea(); address.focusedProperty().addListener(new ChangeListener<Boolean>() { @Override public void changed(ObservableValue<? extends Boolean> arg0, Boo
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");
}
}
});
TextArea address=newtextarea();
address.focusedProperty().addListener(新的ChangeListener()){
@凌驾
public void已更改(observeValue当焦点从对话框返回主舞台时,textarea将再次获得焦点,这将触发再次弹出对话框。您可以从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());
}
// focus to different node on the scene
address.getParent().requestFocus();
// or mySubmitBtn.requestFocus();
} else {
System.out.println("Textfield out focus");
}
}
});
address.focusedProperty().addListener(新的ChangeListener()){
@凌驾
public void已更改(ObservalEvalue我已经尝试过此代码,它工作得非常好
@Override
public void start(Stage stage) {
TextArea address = new TextArea();
address.focusedProperty().addListener(new ChangeListener<Boolean>() {
@Override
public void changed(ObservableValue<? extends Boolean> arg0, Boolean oldPropertyValue, Boolean newPropertyValue) {
TextInputDialog dialog = new TextInputDialog("walter");
dialog.setTitle("Text Input Dialog");
dialog.setHeaderText("Look, a Text Input Dialog");
dialog.setContentText("Please enter your name:");
if (newPropertyValue) {
System.out.println("Old Property : " + oldPropertyValue);
System.out.println("New Property : " + newPropertyValue);
System.out.println("Textfield on focus");
address.getParent().requestFocus();
Optional<String> result = dialog.showAndWait();
System.out.println("Result :" + result);
if (result.isPresent()) {
dialog.getDialogPane().requestFocus();
System.out.println("Your name: " + result.get());
}
}
}
});
Scene scene = new Scene(new VBox(address), 200, 200);
stage.setScene(scene);
stage.show();
address.getParent().requestFocus();
}
@覆盖
公众假期开始(阶段){
TextArea address=新TextArea();
address.focusedProperty().addListener(新的ChangeListener()){
@凌驾
public void已更改(ObservalEvalue每当我单击对话框时,它的输出为“Textfield out focus”。我无法关闭对话框。我认为它处于某种循环中。@VikasSingh,通过“每当我单击对话框时”您的意思是,右击“取消”或“确定”按钮?添加地址.getParent().requestFocus())
行,与上面的答案完全相同,然后再试一次。我尝试了与您编写的相同的代码,但不起作用。是的,对话框没有被任何按钮关闭。@VikasSingh我添加了一个完整的示例代码。这样,对话框就不会不断弹出。您是否也可以尝试一下,然后与您的进行比较?
@Override
public void start(Stage stage) {
TextArea address = new TextArea();
address.focusedProperty().addListener(new ChangeListener<Boolean>() {
@Override
public void changed(ObservableValue<? extends Boolean> arg0, Boolean oldPropertyValue, Boolean newPropertyValue) {
TextInputDialog dialog = new TextInputDialog("walter");
dialog.setTitle("Text Input Dialog");
dialog.setHeaderText("Look, a Text Input Dialog");
dialog.setContentText("Please enter your name:");
if (newPropertyValue) {
System.out.println("Old Property : " + oldPropertyValue);
System.out.println("New Property : " + newPropertyValue);
System.out.println("Textfield on focus");
address.getParent().requestFocus();
Optional<String> result = dialog.showAndWait();
System.out.println("Result :" + result);
if (result.isPresent()) {
dialog.getDialogPane().requestFocus();
System.out.println("Your name: " + result.get());
}
}
}
});
Scene scene = new Scene(new VBox(address), 200, 200);
stage.setScene(scene);
stage.show();
address.getParent().requestFocus();
}