Close modal window by clicking in it (like Window.Notification error)

Hi,

I see that it’s possible to click anywhere in a
Window.Notification.TYPE_ERROR_MESSAGE
to close the notification. In this case it is not necessary to hit the small "
x
" to close it.

My question is if there is a nice way to implement this behavior in a modal window. I would like to build an information window but with more modifications than allowed by
Window.Notification
. It would be nice if the user could close this window by clicking
anywhere
in it.

Any ideas?

Thanks, Thorsten

Hello,
You can do that by adding a click listener on the layout of your modal window.
see how to add click listener to layout,
http://demo.vaadin.com/sampler#ClickableLayoutBasic

Thanks!

Works for me like this:


public class MyModalWindow extends Window {
   public MyModalWindow(String caption) {
      super(caption);
      setModal(true);

      VerticalLayout layout = new VerticalLayout();
      layout.setSizeFull();

      layout.addComponent(new Label("---- Click me to close the window ----"));
      layout.addListener(new LayoutClickListener() {
         public void layoutClick(LayoutClickEvent event) {
            close();
         }
      });

      addComponent(layout);
   }
}

This works better … (using
Listener
on
Panel
to get a larger click area)


public class MyModalWindow extends Window {
   public MyModalWindow(String caption) {
      super(caption);
      setModal(true);

      VerticalLayout layout = new VerticalLayout();
      layout.setSizeFull();

      layout.addComponent(new Label("---- Click me to close the window ----"));
      addListener(new MouseEvents.ClickListener() {
         public void click(ClickEvent event) {
            close();
         }
      });

      addComponent(layout);
   }
}

Great snippet, I imagine many would find this useful! We should add it somewhere… Sampler, perhaps…?

Thanks!

// Marc