Search code examples
gwtpopuppanelparent-child

GWT Close Popup Panels properly?


I want close a Popup Panel clicking an anchor, but this anchor could be inside several panels, then go through parents is not a good idea.

How can I get the Popup Panel where the anchor is?


Solution

  • What about just passing a variable with the PopupPanel into the other panels?

    public class PanelWithPopup extends Composite
    {
        FlowPanel thisPanel = new FlowPanel();
        PopupPanel popup = new PopupPanel();
        SomeOtherPanel otherPanel;
    
        public PanelWithPopup()
        {
            // pass the popup panel to the SomeOtherPanel
            otherPanel = new SomeOtherPanel(popup);
    
            thisPanel.add(otherPanel);
            initWidget(thisPanel);
        }
    }
    
    public class SomeOtherPanel
    {
        PopupPanel popup;
    
        public SomeOtherPanel(PopupPanel p)
        {
            this.popup = p;
        }
    
        void hidePopup()
        {
            popup.hide();
        }
    }
    

    Or, if the other panels were defined inside the main panel (i.e. if the SomeOtherPanel was defined within the PanelWithPopup), you could access the PopupPanel popup directly.