Resize Main Browser Window Listener?

I been trying to find how to make vaadin listen to the main browser resize. I tried to add a ResizeListener to the main Window I attached but it does not seem to fire the listener.

Anyone have any clue how to do this?

Have you set the window to be immediate? Otherwise the event will be queued and sent when the next immediate event happens.

The following code shows a notification when the window is resized:

      Window w = new Window("Window with resize listener");
        w.setImmediate(true);
        w.addListener(new Window.ResizeListener() {

            public void windowResized(ResizeEvent e) {
                getMainWindow().showNotification("Resized!");

            }
        });

        setMainWindow(w);

Thanks, that fixed it.

Does anybody know how actually capture the resized window size (e.g. in pixels)? No appropriate getters in
Window.ResizeEvent
:frowning:

getMainWindow().getWidth/Height should return just that.

Thank you! That helped. I thought, that
getMainWindow().getWidth()
would return something like 100%.

Before the first update from the client to the server has been received (after application startup), that is probably the case. The client sends the pixel values back as soon as it knows them (if the main window is in immediate mode, otherwise some other event will pass the values back to server).

Hi Jouni,

I am using CssLayout as a root layout and want to achieve a listener on Browser / Brower Window Resize. I dont want to use Window here, Please suggest how to achieve it.

In Vaadin 7, use Page.getCurrent().addBrowserWindowResizeListener(…).

Thanks it helped me!

Hi all,
Isn’t this sending too many events? For me, having an simple if condition in the listener is taking it’s time to process if you reized the browser dragging it’s edge.
Is there a simple solution to avoid sending an event until you drop it?
Thanks in advance
Fran

Thanks for asking this Francisco! I’d also like to know if there is a simple solution to avoid sending a resize event until the user stops the resize action.

This doesn’t seem to work (I’ve tested in Chrome). Listener is never called.
I have found bug #10055 “Page.addBrowserWindowResizeListener is not working”, which is claimed to be fixed, but it is still not working.

I did it like this:
JavaScript.getCurrent().addFunction(“browserWindowResized”, e → …)
Page.getCurrent().getJavaScript().execute(“window.onresize = function() { browserWindowResized(); };”);