Search code examples
gwtuibindergwt2

How to close a window in GWT


I was required to put a "Done" button in a GWT Composite (despite already having the close icon), it should simply close the window upon clicking. Unfortunately, I can't find a .close() method to implement it. How can it be done?

enter image description here

I have a UserDialog class that contains a Composite component, which I named UserComposite. UserDialog extends to CustomDialogBox, which extends to DialogBox class:

public class UserDialog extends CustomDialogBox {    
    private UserComposite c = new UserComposite();
    // more codes here
    private FlowPanel getFlowPanel() {
        if (p instanceof Panel && c instanceof Composite) {
            p.setSize(WIDTH, HEIGHT);
            p.add(c);
        }
        return p;
    } 
}

and then this is my UserComposite

public class UserComposite extends Composite {
   // codes here
   @UiHandler("doneButton")
   void onDoneButtonClick(ClickEvent event) {
      this.removeFromParent();
   }
}

I tried removeFromParent() but the UserComposite was only removed from parent which resulted to an empty DialogBox.

enter image description here


Solution

  • You need to hide the dialog, not the composite. One way to do this is to pass a reference to the dialog box to the UserComposite constructor, and then use that reference to call hide() on the dialog. It could be something along these lines:

    public class UserDialog extends CustomDialogBox {
        private UserComposite c = new UserComposite(this);
        ...
    }
    
    public class UserComposite extends Composite {
        private DialogBox parentDialog;
    
        public UserComposite(DialogBox parentDialog) {
            this.parentDialog = parentDialog;
        }
    
        @UiHandler("doneButton")
        void onDoneButtonClick(ClickEvent event) {
            parentDialog.hide();
        }
    }