rubdottocom
rubdottocom

Reputation: 8298

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?

Upvotes: 0

Views: 1882

Answers (2)

Craigo
Craigo

Reputation: 3717

If you want to close all open popup panels. Eg: If the user clicks the back button, or your anchor button triggers a page change. You can use this method:

public static void closeAllPopups() {
    for (int i=0; i<RootPanel.get().getWidgetCount(); i++) {
        if (RootPanel.get().getWidget(i) instanceof PopupPanel) {
            PopupPanel popupPanel = (PopupPanel)RootPanel.get().getWidget(i);
            Scheduler.get().scheduleDeferred(() -> popupPanel.hide());
        }
    }
}

Upvotes: 0

janhink
janhink

Reputation: 5023

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.

Upvotes: 2

Related Questions