tags:

views:

135

answers:

1

How do I remove a JPanel (or any other JComponent) from a CardLayout? I do not have direct access to the Component I want to remove, but I have the index (the one used to show the Panel, when we call cardLayout.show(parentComponent, index);).

+2  A: 

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

Component[] components = parentComponent.getComponents();

for(int i = 0; i < components.length; i++) {
    if(components.getName().equals(index)) {
        cardLayout.removeLayoutComponent(components[i]);
    }
}
Alex
Yes by index I mean the string used when adding the component to the cardlayout. Thanks for your solution, I tried it out and it worked.
Spi1988