如何解决如何让 Vaadin 通知立即出现
所以我有一个方法 createDataOffer()
需要几秒钟才能完成,我想让用户知道系统正在做某事,点击按钮时显示通知。
但是只有在方法完成后才会出现通知。那么如何让通知立即出现呢?我已经尝试为相应的调用添加两个单独的点击侦听器,但结果相同。
Button createButton = new Button("Create");
Notification startNotification = new Notification("Creating data offer",3000);
createButton.addClickListener(evt -> {
startNotification.open();
createDataOffer(price);
});
解决方法
与 André Schild 的回答类似,Vaadin forum 上有一个线程可能会对您有所帮助。
线程中提到了此代码片段:
Button testButton = new Button("Test UI access",click -> {
Thread backgroundThread = new Thread(() -> {
// anything happening inside this thread will not be updated to the ui automatically,and needs to be pushed to the ui.
Thread.sleep(2000); // wait 2 seconds to imitate long task
this.ui.access(() -> {
Notification.show("Test notification");
});
}
});
此外,此线程中还提到了 @Push
注释,可能会对您有所帮助。
最好将长时间运行的任务作为一个单独的线程,然后您的 UI 更新将立即显示。
如果你想从长时间运行的线程更新 UI,你必须使用这样的东西:
ui.access(() -> statusLabel.setText(statusText));
https://vaadin.com/docs/flow/advanced/tutorial-push-access.html
,试试下面的
Button createButton = new Button("Create");
Notification startNotification = new Notification("Creating data offer",3000);
createButton.addClickListener(evt -> {
startNotification.open();
new Thread(() -> createDataOffer(price)).start();
});
这样,一旦单击 createButton 就会显示通知。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。