简体   繁体   English

Vaadin 8:如何从另一个线程更新模态表单上的标签值

[英]Vaadin 8: How to update a label value on a modal form from another thread

I am using vaadin 8, and I need to update a label value on a modal.我正在使用 vaadin 8,我需要更新模态上的标签值。

In the listing below, a label value is inside a while(true) cycle being executed from another Thread :在下面的清单中,标签值位于从另一个Thread执行的while(true)循环内:

      modal.show();
        modal.getUI().access(new Runnable() {
                    @Override
                    public void run() {
                        while(true) {
                        sleep(1000)
                        ....
                        break_condition;
                        break;
                        }
                        modal.getLabel().setValue("xxxx")
                    }
        });

During the process, the screen will freeze and the modal windows appears at the end with the label value modified.在此过程中,屏幕将冻结,最后出现模态窗口并修改标签值。 Is it possible to do without freezing the screen?是否可以不冻结屏幕?

A possible solution...一个可能的解决方案...

class TaskCycle implements Runnable {

    MyModal modal;

    public TaskCycle(MyModal modal){
      this.modal = modal
    }

        @Override
        public void run() {
           while(true) {
             sleep(1000)
              ....
             break_condition;
             break;
           }
           modal.getLabel().setValue("xxxx");
           modal.getLabel().getUI().push();
        }    
    }


MyModal modal = new MyModal();
modal.show();

TaskCycle r = new TaskCycle (modal);    
Thread t = new Thread(r);
t.start();

In general, this solution works well but needs of the configuration of a "vaadin server-push" (see the officical documentation).一般来说,这个解决方案效果很好,但需要配置“vaadin server-push”(请参阅​​官方文档)。

Unfortunately I am using Liferay 7 and the push server (of vaadin 8) does not work.不幸的是,我正在使用 Liferay 7 并且推送服务器(vaadin 8)不起作用。

This solution in my case is not the best, because the modal is updated with the new values (provided by the thread), only if you perform a "resize" of the browser window/tab.在我的情况下,此解决方案不是最好的,因为仅当您执行浏览器窗口/选项卡的“调整大小”时,模态才会使用新值(由线程提供)进行更新。

In order to "simulate" the resize it is possible to insert the function:为了“模拟”调整大小,可以插入函数:

setPollInterval(1000);
UIEvents.PollListener poll = new UIEvents.PollListener(){

            @Override
            public void poll(UIEvents.PollEvent event) {
                System.out.println("Poll interval start!!!!!");
            }
        };
UI.getCurrent().addPollListener(poll);

to remove listener:删除侦听器:

UI.getCurrent().removePollListener(poll);

this is a good workaround for all users that can't use the push() function.对于所有不能使用 push() 函数的用户来说,这是一个很好的解决方法。

Try to relax the while loop execution introducing a sleep.尝试放松引入睡眠的 while 循环执行。 This could avoid the screen to freeze:这可以避免屏幕冻结:

Thread.sleep(50);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM