Question

I'm using GWT and I need to have the results of a form submission open in a new window with a set width and height. I can get it to open in a new tab, but don't know how to get it to load in a new window, nor how to set the width and height. Here's my code :

 @UiHandler({"myGWTButton"})
    protected void myGWTButtonClicked(ClickEvent click) {
      click.preventDefault();
      FormPanel form = new FormPanel("_blank");
      form.setAction("www.mydomain.com");
      form.setMethod(FormPanel.METHOD_POST);
      form.setEncoding("multipart/form-data");
      TextArea params0 = new TextArea();
      params0.setName("foo");
      params0.setValue("bar");
      form.setVisible(false);
      form.add(params0);
      RootPanel.get().add(form);
      form.submit();
    }
Was it helpful?

Solution

It seems that I was over-thinking things by trying to use GWT. I solved my problem by relying on native javascript.

 @UiHandler({"myGWTButton"})
    protected void myGWTButtonClicked(ClickEvent click) {
    click.preventDefault();
    openPopupWindow("www.mydomain.com", 'bar');
}

native void openPopupWindow(String url, String val) /*-{
       var windowName = '' + Math.floor(Math.random() * 10000000) ; //we need some name to reference it later, so assign a random number as the name (make sure the number doesn't have a decimal because IE will choke on it)
       var popupWindow =  window.open('', windowName, 'width=550,height=350,resizeable,scrollbars');
       var form = document.createElement("form");
       form.setAttribute("method", "post");
       form.setAttribute("action", url);
       form.setAttribute("target", windowName);
       form.setAttribute("enctype", "multipart/form-data");
       var hiddenField = document.createElement("input");
       hiddenField.setAttribute("type", "hidden");
       hiddenField.setAttribute("name", "foo");
       hiddenField.setAttribute("value", val);
       form.appendChild(hiddenField);
       document.getElementsByTagName('body')[0].appendChild(form);
       while(!popupWindow){
         //wait for popupwindow to open before submitting the form
       }
       form.submit();

    }-*/;

OTHER TIPS

Forms seem to get their results in a separate iframe. Problem is, you can't really interact with this iframe because of cross-site restrictions (SOP). So I'm not sure there's a way to do this.

Could you not use a HTTP request instead?

I have possible solution for you if you can use GET method instead of POST.

@UiHandler({"myGWTButton"})
protected void myGWTButtonClicked(ClickEvent click) {
  click.preventDefault();

  Window.open(new UrlBuilder()
        .setHost("www.mydomain.com")
        .setParameter("foo", "bar")
        .buildString(), "_blank", "width=200,height=200");
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top