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]); } }
source share