when I navigate from one Vaadin GUI class to another Vaadin GUI class which having an asynchronous update on h1 tag, the asynchronous update does not show any changes on UI until I do something in the same view (like clicking inside an edit box in the same view)
This asynchronous update does work only if I directly access to the GUI class interface
///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();
}
}
}
}
}