【发布时间】:2012-11-27 14:37:22
【问题描述】:
我正在开发 JavaFX 应用程序,在我的场景中是显示在 JavaFX 中创建的密码提示,它使用两个选项 OK 和 Cancel 输入密码。我已经返回了用户输入的密码。
我显示密码对话框的类是 -
public static String showPasswordDialog(String title, String message, Stage parentStage, double w, double h) {
try {
Stage stage = new Stage();
PasswordDialogController controller = (PasswordDialogController) Utility.replaceScene("Password.fxml", stage);
passwordDialogController.init(stage, message, "/images/password.png");
if (parentStage != null) {
stage.initOwner(parentStage);
}
stage.initModality(Modality.WINDOW_MODAL);
stage.initStyle(StageStyle.UTILITY);
stage.setResizable(false);
stage.setWidth(w);
stage.setHeight(h);
stage.showAndWait();
return controller.getPassword();
} catch (Exception ex) {
return null;
}
我的代码在哪里显示密码提示如下,实际上这个提示会显示在其他 UI 上,所以我需要将它包含在 Platform.runlater() 中,否则它会抛出 Not on FX application thread。我需要显示此密码提示,直到我得到正确的提示。如果我在 runlater 中包含显示密码,如何获取密码值。
还有其他更好的方法吗?
final String sPassword = null;
do {
Platform.runLater(new Runnable() {
@Override
public void run() {
sPassword = JavaFXDialog.showPasswordDialog(sTaskName + "Password", "Enter the password:", parentStage, 400.0, 160.0);
}
});
if (sPassword == null) {
System.out.println("Entering password cancelled.");
throw new Exception("Cancel");
}
} while (sPassword.equalsIgnoreCase(""));
【问题讨论】: