candela
candela

Reputation: 23

auto closing Vaadin window after delay

I wanted to create my own message box class so that I can create different types of messages (w/o buttons e.g.). For the tray messages I want an auto close after lets say 5sec delay.

I've searched the internet: I found very old posts suggesting ProgressIndicator or some "newer" ones (4yo) with push/poll/threading. I really wonder if there is a neat solution meanwhile in Vaadin 7.6. Push/poll seemed a little too complicated for me to understand the mechanics (beginner...).

Upvotes: 1

Views: 1145

Answers (2)

Pere
Pere

Reputation: 1075

Well, as noone has given the proper answer, you could do something like this with a Timer and a TimerTask:

Window wd = new Window("Your autoclosing window");
// do stuff
Timer delayer = new Timer();
retrasador.schedule(new TimerTask(){
    @Override
    public void run() {
        UI.getCurrent().access(new Runnable() {
            @Override
            public void run() {
                wd.close();
            }
        });
    }
}, 10000); // auto closes after 10 s

Note that you should probably take care of your Timer after you're done with it.

Upvotes: 0

kukis
kukis

Reputation: 4634

TLDR: Either you use Push/poll techniques or you need to write your brand new component in GWT. The choice is yours

To fully understand why is that you need to know how Vaadin or/and web applications work. Lets take your example: you want to tell the browser in some way to close the window after some delay (5 sec). You can achieve desired effect in two different ways:

  • use setTimeout in JavaScript
  • send a message from the server to the browser

setTimeout in Vaadin: of course there is no such method in Vaadin since this framework uses pre-compiled GWT components. So you would need to either find a component in Vaadin addons repo or write your own. GWT is not JavaScript - this is true but you can still attach your own JavaScript file with any GWT component.

Sending a message from server to the browser means basically Push/Polling. No other way around. Vaadin actually support both so the only thing you need is to learn about them.

There is also a hack but I wouldn't recommend this approach. In general you can execute pure JavaScript code at runtime using:

JavaScript.getCurrent().execute("alert('Hello')");

But should you want to go this way you will have hard time managing your popups since Vaadin framework will not expect you to close windows that he is responsible for.

Upvotes: 3

Related Questions