Close Modal Window from within a Form

I have a modal Window which displays a Form, The form has a Close button, but I don’t know how to close this Form and Modal Window.

I’ve tried form.destroy(); but that does not do anything.

I want to have the button on the form and when user clicks on the button I want the Modal Window to close along with the form.

Can someone show me how to do that?

Thank You.

Peter

i either use removeWindow(“Window Name”); or getApplication.getMainWindow.removeWindow(“Window Name”)

it works for my project tracking application

I don’t see any removeWindow methods that take String as the parameter.

but I’ve tried the following and it does not work either. The code runs fine, no errors, but modal window does not close.

	Button apply = new Button("Close", new Button.ClickListener() {
		public void buttonClick(ClickEvent event) {

			Window w = getWindow();
			getApplication().removeWindow(w);
		}
	});

I have a class where I display a data grid and if user double clicks on the row I create a modal Window and on this window I add another VerticalLayout class and in this class I have the Close button which does not work.

The Close button works fine if I create the Button on the Modal Window, but it does not work if I create the button in the VerticalLayout Class

There are two types of windows: browser level windows (e.g. main window) and sub windows. Browser level windows are added to and removed from the application. Sub windows are added to and removed from their parent (browser level) window. What you need to do is remove the sub window from its parent window, something like

((Window)window.getParent()).removeWindow(window);

Would someone be so kind and implement the “Window.closeThisBloodyWindowForMeWouldYa()” method and get us rid of having to worry about the parent once and for all!

k thanks! :slight_smile:

I tried that, but getting the following error,

com.vaadin.event.ListenerMethod$MethodException
Cause: java.lang.ClassCastException: com.vaadin.ui.VerticalLayout cannot be cast to com.vaadin.ui.Window
at com.vaadin.event.ListenerMethod.receiveEvent(ListenerMethod.java:507)
at com.vaadin.event.EventRouter.fireEvent(EventRouter.java:161)
at com.vaadin.ui.AbstractComponent.fireEvent(AbstractComponent.java:1107)
at com.vaadin.ui.Button.fireClick(Button.java:341)

My ‘window’ is

public class DataForm extends VerticalLayout {

VerticalLayout is probably the layout in the window, definitely not the window itself. Use the same Window object as you use to display the modal window.

Thank You !
That Worked.

here’s the code I used:

((Window) getWindow().getParent()).removeWindow(getWindow());

How should i close window by hitting ESC key on keyboard.? Is thereb any way to do this?

package com.example.kbtest;

import com.vaadin.Application;
import com.vaadin.event.Action;
import com.vaadin.event.ShortcutAction;
import com.vaadin.ui.Button;
import com.vaadin.ui.Window;
import com.vaadin.ui.Button.ClickEvent;

@SuppressWarnings("serial")
public class KbtestApplication extends Application {

    Button newDialogButton = new Button("New dialog",
            new Button.ClickListener() {
                public void buttonClick(ClickEvent event) {
                    getMainWindow().addWindow(
                            new ClosableDialog("Close with esc"));
                }
            });

    @Override
    public void init() {
        final Window mainWindow = new Window("Kbtest Application");
        mainWindow.addComponent(newDialogButton);
        newDialogButton.focus();
        setMainWindow(mainWindow);
    }

    static final Action esc = new ShortcutAction("Close window",
            ShortcutAction.KeyCode.ESCAPE, null);
    static final Action[] actions = new Action[]
 { esc };

    class ClosableDialog extends Window implements Action.Handler {

        ClosableDialog(String caption) {
            setModal(true);
            setCaption(caption);
            addActionHandler(this);
            // Something must be focused in the window to capture shortcuts
            // This is a known limitation http://dev.vaadin.com/ticket/4384
            Button ok = new Button("Ok");
            addComponent(ok);
            ok.focus();
        }

        public void handleAction(Action action, Object sender, Object target) {
            if (action == esc) {
                ((Window) getParent()).removeWindow(this);
            }
        }

        public Action[] getActions(Object target, Object sender) {
            return actions;
        }
    }
}

I had a slightly different result- your code gave me an error. I have a form, to which I’ve added a Save and Cancel button beneath inside a modal Window.

What worked for me was this (in the button click event handler):


   public void onClickSaveNewContent(Button.ClickEvent event)
      {
      ...
      Window win = event.getButton().getWindow() ;
      win.getParent().removeWindow(win) ;
      ...
      }

Hope that helps others.
I agree with Jouni BTW :smiley: