Remove JComponent from CardLayout

How to remove JPanel (or any other JComponent ) from CardLayout ? I don't have direct access to the component that I want to remove, but I have an index (the one that was used to display the panel when we call cardLayout.show(parentComponent, index); ).

+4
source share
1 answer

When you say an index, you mean the name (String) of the component when it was inserted, right? I don’t know of any elegant way to do this, but you can try to get all the components in this container (parentComponent) and try to find one that has the same name as your index. For instance:

 Component[] components = parentComponent.getComponents(); for(int i = 0; i < components.length; i++) { if(components[i].getName().equals(index)) { cardLayout.removeLayoutComponent(components[i]); } } 
+4
source

All Articles