【发布时间】:2019-07-31 16:04:40
【问题描述】:
我创建了 Vaadin(13) 异步更新以使用 @push 注释(异步更新)更改垂直布局中的 H1 Vaadin 元素,它确实有效但不会更新对 h1 元素的更改,直到我在同一个窗口上执行某些操作,例如单击选项卡。我编写了代码以通过线程动态更改 h1 元素文本,该线程在页面加载后使用重写的 onAttach 方法自动运行。
@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();
}
}
}
}
}
【问题讨论】:
-
你开启推送了吗?
-
@AndréSchild 是的,使用“推送”注释
标签: vaadin