从javafx平台runlater返回结果 [英] Return result from javafx platform runlater
问题描述
我正在研究JavaFX应用程序,在我的场景中是显示在JavaFX中创建的密码提示,它带有两个选项的密码确定
和取消
。我已经返回了用户输入的密码。
I am working on JavaFX application, in my scenario is to show a password prompt created in JavaFX which takes password with two option OK
and Cancel
. I have returned the password entered by user.
我的显示密码对话框的类是 -
My class of showing password dialog is -
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()
中包含这个,否则它会抛出不在FX应用程序线程上
。我需要这个密码提示才能显示,直到我得到正确的密码提示。如果我在runlater中显示密码,我如何获得密码值。
My code where to show password prompt is below, actually this prompt will be shown over other UI, so I need to inclose this inside Platform.runlater()
, otherwise it throws Not on FX application thread
. I need this password prompt to be shown until I get correct one. How can I get value of password if I inclosed showing password inside runlater.
还有其他更好的方式吗?
Is there any other better way?
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(""));
推荐答案
我建议将代码包装在 FutureTask
对象。 FutureTask
是一个有用的构造(除其他外),用于在一个线程上执行一部分代码(通常是工作者,在您的情况下是事件队列)并在另一个线程上安全地检索它。 FutureTask#get
将被阻止,直到 FutureTask#run
被调用,因此您的密码提示符如下所示:
I'd recommend wrapping the code within a FutureTask
object. FutureTask
is a construct useful (among other things) for executing a portion of code on one thread (usually a worker, in your case the event queue) and safely retrieving it on another. FutureTask#get
will block until FutureTask#run
has been invoked, therefore your password prompt could look like this:
final FutureTask query = new FutureTask(new Callable() {
@Override
public Object call() throws Exception {
return queryPassword();
}
});
Platform.runLater(query);
System.out.println(query.get());
作为 FutureTask
实现Runnable,你可以通过它直接到 Platform#runLater(...)
。 queryPassword()
将在事件队列中被忽略,并且随后调用get block直到该方法完成。当然,您需要在循环中调用此代码,直到密码实际匹配为止。
As FutureTask
implements Runnable, you can pass it directly to Platform#runLater(...)
. queryPassword()
will be inokved on the event queue, and the subsequent call to get block until that method completes. Of course, you will want to invoke this code in a loop until the password actually matches.
这篇关于从javafx平台runlater返回结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!