vaadin对h1元素的异步更新不会动态更改h1元素

时间:2019-07-31 16:04:40

标签: vaadin

我创建了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();
                    }


                }
            }

        }
    }

2 个答案:

答案 0 :(得分:2)

似乎您正在关注https://vaadin.com/docs/v10/flow/advanced/tutorial-push-access.html上的推送教程。

通常这应该起作用。

您是否可能忘记了视图中的@Push-注解? (请参阅链接的教程上的PushyView

答案 1 :(得分:2)

正如前面的答案和评论中已经指出的那样,您很可能在视图上缺少@Push注释。我已经复制了您的代码,它可以按预期工作(H1已更新)。您能否验证标注是否在位,如果可以,可以提供我们可以重现问题的完整示例吗? 一个有效的示例:


@Route
@PWA(name = "Project Base for Vaadin Flow with Spring", shortName = "Project Base")
@Push
public class MainView extends VerticalLayout {

    private FeederThread thread;


    public MainView(@Autowired MessageBean bean) {
        Button button = new Button("Click me",
                e -> Notification.show(bean.getMessage()));
        add(button);
    }

    @Override
    protected void onAttach(AttachEvent attachEvent) {
        add(new Span("Waiting for updates"));
        H1 h1=new H1("Text");
        add(h1);

        // Start the data feed thread
        thread = new FeederThread(attachEvent.getUI(), h1);
        thread.start();
    }

    @Override
    protected void onDetach(DetachEvent detachEvent) {
        // Cleanup
        thread.interrupt();
        thread = null;
    }
    private static class FeederThread extends Thread {
        private final UI ui;
        private  final H1 element;

        private int count = 0;

        public FeederThread(com.vaadin.flow.component.UI ui,H1 element) {
            this.ui = ui;
            this.element = element;
        }
        @Override
        public void run() {
            try {
                // Update the data for a while
                while (count < 10) {
                    // Sleep to emulate background work
                    Thread.sleep(500);
                    String message = "This is update " + count++;

                    ui.access(() -> element.setText(String.valueOf(message)));
                }

                // Inform that we are done
                ui.access(() -> {
                    ui.access(() -> element.setText("DOONE"));
                });
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
}