[英]How to setText from a CompletableFuture
我在通过 completableFuture 设置窗口名称时遇到问题。 我需要获取用户所在的帐户,并以此命名主要的 window。
MainWindow mainWindow = this;
...
CompletableFuture.supplyAsync(() -> {
while(true) {
try {
// wait until the account is chosen
if (MainWindow.idAccount > 0 ) {
ResultSet rs = // some request to the db
rs.next();
return rs.getString(1);
}
} catch (Exception e) {
System.out.println("Error while getting the address of the account. " + e.getMessage());
}
}
}).thenAccept(account -> {
System.out.println(account);
mainWindow.setText("POS - " + account);
});
一切正常,实际上在.thenAccept() 控制台打印出正确的帐户地址。 但是,文本从未设置过。
可能与线程之间的访问有关吗? 提前致谢。
我假设 MainWindow 是一个 Swing 组件。 Swing 组件在 EDT 中更新。
SwingUtilities.invokeLater(() -> mainWindow.setText("POS - " + account));
附注:如果它是一个框架,您可以尝试使用setTitle
设置标题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.