Asynchronously update vaadin components

血红的双手。 提交于 2019-12-06 07:21:54

问题


I have this code for updating vaadin button's caption every 3 seconds.

TimerTask tt = new TimerTask() {

    @Override
    public void run() {
        try {
            logger.debug("adding l to button's caption");
            btn.setCaption(eventsButton.getCaption() + "l");
        } catch (Exception ex) {
            logger.error(ex.getMessage());
        }
    }
};
Timer t = new Timer(true);
t.scheduleAtFixedRate(tt, 0, 3000);

However, it can't change button's caption although it is executed every 3 seconds(judging by the log file). How can I access vaadin's GUI components from another thread?


回答1:


There's an addon named ICEPush which does exactly what I needed.

https://vaadin.com/directory#addon/icepush




回答2:


A reasonably comprehensive discussion of the problem, and the various solutions can be found here; Redux: 'vanilla' Vaadin simply follows a user initiated request-response paradigm.

You'll need to use an add-on to initiate changes in the browser from the server.

Aside : you should synchronize on the application object when updating components from your own threads (as opposed to the normal request thread) - as you may get 'Sync' errors.




回答3:


Because of the way Vaadin works, asynchronous UI changes made on the server side are not reflected on the client. The refresher addon makes it possible to make UI changes, even if the user does not start a transaction.

final Refresher refresher = new Refresher();
refresher.setRefreshInterval(3000);
addComponent(refresher);

refresher.addListener(new RefreshListener() {    
    @Override
    public void refresh(final Refresher source) {
        try {
            logger.debug("adding l to button's caption");
            btn.setCaption(eventsButton.getCaption() + "l");
        } catch (Exception e) {
            logger.error(e.getMessage());
        }
    }
}


来源:https://stackoverflow.com/questions/11553576/asynchronously-update-vaadin-components

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!