当加载到浏览器中的网页调用Window.alert或window.confirm时,我使用JavaFX / JXBrowser显示警告/对话框。但是,我无法弄清楚如何将确认对话框的结果(true / false)返回给JS。由于alert.showAndWait()是一个阻塞函数,JS应该等待这个结果。但是,showAndWait也在Platform.runLater中运行,因此我无法返回结果。如果没有编写JS函数来执行真/假代码并根据showAndWait的结果调用它们,还有其他选择吗?
<table>
<tr ng-repeat="group in currentGroup">
<td ng-repeat="list in group.lists">{{list.name}}</td>
</tr>
</table>
答案 0 :(得分:1)
您可以使用以下方法:
@Override
public CloseStatus onConfirmation(DialogParams params) {
final AtomicReference<CloseStatus> status = new AtomicReference<CloseStatus>();
final CountDownLatch latch = new CountDownLatch(1);
Platform.runLater(new Runnable() {
@Override
public void run() {
Alert alert = new Alert(AlertType.CONFIRMATION);
alert.setTitle("Yes/No");
alert.setHeaderText(null);
alert.setContentText("Yes/No");
Optional<ButtonType> result = alert.showAndWait();
if (result.isPresent()) {
if (result.get() == ButtonType.YES) {
status.set(CloseStatus.OK);
} else {
status.set(CloseStatus.CANCEL);
}
} else {
System.out.println("No result!");
}
}
});
try {
latch.await();
} catch (InterruptedException ignored) {
Thread.currentThread().interrupt();
}
return status.get();
}