使用UI.getCurrent()。navigate从另一个类导航时,一个类中的异步更新不起作用

时间:2019-08-03 06:19:55

标签: java spring vaadin

当我从一个Vaadin GUI类导航到另一个在h1标签上具有异步更新的Vaadin GUI类时,异步更新不会显示UI上的任何更改,除非我在同一视图中进行了某些操作(例如在编辑框内单击)在同一视图中

仅当我直接访问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();
                    }


                }
            }

        }
    }

1 个答案:

答案 0 :(得分:0)

这是一个已知问题:https://github.com/vaadin/flow/issues/5759。解决方法是,您可以为所有layout类拥有一个公共的父@Route,然后将@Push放在该类上。另外,您也可以使用例如UI.getCurrent().getPushConfiguration().setPushMode(PushMode.AUTOMATIC)