当我从一个 Vaadin GUI 类导航到另一个在 h1 标记上具有异步更新的 Vaadin GUI 类时,异步更新不会在 UI 上显示任何更改,直到我在同一 View 中执行某些操作(例如在编辑框内单击)在同一 View 中)
仅当我直接访问 GUI 类界面时,此异步更新才有效
///navigate from class code
public class WaitForPlayers extends VerticalLayout {
..........................
UI.getCurrent().navigate(Playboard.class);
}
//navigate to class, with an asynchronous update
@Push
public class Playboard extends VerticalLayout
{
private H1 timerc;
private FeederThread thread;
public Playboard() throws ExecutionException, InterruptedException{
generateGUI();
}
private void generateGUI() throws ExecutionException, InterruptedException {
setSizeFull();
setDefaultHorizontalComponentAlignment(Alignment.CENTER);
addClassName("main-view");
H1 header = new H1("Stock Market Simulation - Playboard");
header.setWidthFull();
header.setHeight("10%");
header.getElement().getThemeList().add("dark");
add(header);
HorizontalLayout contents = new HorizontalLayout();
contents.setSizeFull();
contents.addClassName("content-view");
VerticalLayout player = new VerticalLayout();
player.setWidth("25%");
player.setHeightFull();
player.addClassName("player-view");
VerticalLayout playboard = new VerticalLayout();
playboard.addClassName("playboard-view");
playboard.setWidth("75%");
player.setDefaultHorizontalComponentAlignment(Alignment.START);
Image p1Img = new Image("frontend/icons/userLogo.png","player");
p1Img.setWidth("150px");
p1Img.setHeight("150px");
H4 playerName = new H4("Player");
H4 totalWorth = new H4("Cash On Hand : ");
H4 round = new H4("Round :");
H4 timeR = new H4("Time Remaining");
timerc = new H1("30s");
player.add(p1Img,playerName,totalWorth,round,timeR,timerc);
player.setHorizontalComponentAlignment(Alignment.CENTER,timerc);
player.setHorizontalComponentAlignment(Alignment.CENTER,p1Img);
contents.add(player,playboard);
add(contents);
Tab buy = new Tab("Buy");
Tab sell = new Tab("Sell");
Tabs tabs = new Tabs(buy, sell);
tabs.setFlexGrowForEnclosedTabs(1);
playboard.add(tabs);
}
@Override
protected void onAttach(AttachEvent attachEvent) {
// Start the data feed thread
thread = new FeederThread(attachEvent.getUI(),timerc);
thread.start();
}
@Override
protected void onDetach(DetachEvent detachEvent) {
thread.interrupt();
thread = null;
}
//Thread
private static class FeederThread extends Thread {
private final com.vaadin.flow.component.UI ui;
private final H1 element;
private int count = 30;
public FeederThread(com.vaadin.flow.component.UI ui,H1 element) {
this.ui = ui;
this.element = element;
}
@Override
public void run() {
while (count>0){
try {
Thread.sleep(1000);
ui.access(()-> {
element.setText(String.valueOf(count));
});
count--;
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
}
最佳答案
这是一个已知问题:https://github.com/vaadin/flow/issues/5759 。作为解决方法,您可以为所有 @Route
类使用通用的父 layout
,然后将 @Push
放在该类上。或者,您可以使用例如以编程方式启用推送UI.getCurrent().getPushConfiguration().setPushMode(PushMode.AUTOMATIC)
。
关于java - 使用 UI.getCurrent().navigate 从另一个类导航时,类中的异步更新不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57335948/