Notify parent panel

I have one panel inside another panel, the inner panel has a button. How do I notify the parent panel the button was clicked in the child panel. I want to hide the inner panel when the button is clicked from the parent panel.

Thank You

Peter

pass a ClickListener that is aware of the parent panel instance to the inner panel, for example as a constructor argument. The inner panel can attach the ClickListener to the Button and the ClickListener can then call upon a function of the parent panel.

Can someone show me code example of this or point me to an example?

Thank You

Here are two example how to achieve what you want:

This first implementation does what Jens suggest, you pass the ClickListener to the second panel as a constructor parameter. Note that to be able to remove the component from the outer panel, you need both a reference to the outer panel and to the inner panel. In this example I’ve stored the references in class variables. Note that I’ve actually used a HorizontalLayout for what you called your “outer panel”, I was too lazy to change it :slight_smile:


public class TestcaseApplication extends Application implements ClickListener {
    private static final long serialVersionUID = 75232258896642392L;

    private final HorizontalLayout mainLayout = new HorizontalLayout();
    private final YourPanel panel = new YourPanel(this);

    @Override
    public void init() {
        setTheme("example");
        Window mainWindow = new Window("Playground Application");
        setMainWindow(mainWindow);

        mainWindow.setContent(mainLayout);

        mainLayout.addComponent(panel);
    }

    public void buttonClick(ClickEvent event) {
        mainLayout.removeComponent(panel);
    }

    public class YourPanel extends Panel {

        public YourPanel(ClickListener listener) {
            super();
            addComponent(new Button("Remove", listener));
        }

    }

}

Antoher example is to implement the ClickListener directly in the inner panel. In the buttonClick method, I just call getParent() (returns the out layout) and then removes itself from that layout.


public class TestcaseApplication extends Application {
    private static final long serialVersionUID = 75232258896642392L;

    @Override
    public void init() {
        setTheme("example");
        Window mainWindow = new Window("Playground Application");
        setMainWindow(mainWindow);

        HorizontalLayout mainLayout = new HorizontalLayout();
        mainWindow.setContent(mainLayout);

        mainLayout.addComponent(new YourPanel());
    }

    public class YourPanel extends Panel implements ClickListener {

        public YourPanel() {
            super();
            addComponent(new Button("Remove", this));

        }

        public void buttonClick(ClickEvent event) {
            ((ComponentContainer) getParent()).removeComponent(this);
        }
    }

}

Thank You very much !!!