我不能强制更新要从线程(FeederThread)更新的progressBar。
应该按下按钮,下一个流开始,progressBar应该出现。
我使用Vaadin Flow和SpringBoot
我的带有ProgressBar和button(startBtn)的用户界面:
@SpringComponent
@UIScope
@Push
@Route("push")
public class UploadEditor extends VerticalLayout {
private FeederThread feederThread;
private ProgressBar bar;
private void progresBarChanche(boolean value) {
bar.setVisible(value);
bar.setIndeterminate(value);
}
@Override
protected void onAttach(AttachEvent attachEvent) {
}
public UploadEditor() {
setup();
}
private void setup() {
VerticalLayout progresLayout = new VerticalLayout();
bar = new ProgressBar();
progresLayout.add(bar);
bar.setVisible(false);
HorizontalLayout layoutBtn = new HorizontalLayout();
Button startBtn = new Button(" Start Thread", VaadinIcon.DATABASE.create());
startBtn.setSizeFull();
layoutBtn.add(startBtn);
layoutBtn.setSizeFull();
startBtn.addClickListener((event) -> {
feederThread = new FeederThread(getUI().get(), this);
feederThread.start();
});
add(progresLayout, layoutBtn);
setAlignItems(FlexComponent.Alignment.CENTER);
}
}
和线程:
private class FeederThread extends Thread {
private final UI ui;
private final UploadEditor view;
public FeederThread(UI ui, UploadEditor view) {
this.ui = ui;
this.view = view;
}
@Override
public void run() {
int count = 0;
try {
ui.access(() -> {view.progresBarChanche(true); ui.push();});
while (count < 10) {
// Sleep to emulate background work
Thread.sleep(500);
count++
}
// Inform that we are done
ui.access(() -> {
view.progresBarChanche(false);
});
}catch (InterruptedException e){
e.printStackTrace();
}
}
}
怎么了? PS我使用Vaadin Flow和SpringBoot