How to remove UI components from a button event within component hierarchy

Apologies if this is really simple but I’m looking for some guidance on removing UI components within version 7:

I created a login form as a panel and within that I have a button with an event listener. When the button is clicked I want to login to the application and then load a home page and remove the login panel.

I can find the panel as an ancestor to the button event. How do I remove it? Does it matter that the code is executing within the button click event when it is deleted?

Thanks

The best way is to use the Navigator; among other things it handles the view changes for you (although you might need to customize the handling, depending on the use case).

Another way is to store the parent of the login panel as a field in your UI class (call it contentContainer), then just call contentContainer.removeComponent(loginPanel) - or you can do contentContainer.replaceComponent(loginPanel, homePanel) to kill two birds with one stone.

You can also just do ((ComponentContainer) loginPanel.getParent()).removeComponent(loginPanel).

Ah thanks - easy when you know how!