Search code examples
javaswingjpanelchildrenjcomponent

Count number of Children in JPanel


I have a JPanel, to which I have added an unknown number of JComponents. How can I count the number of JComponents in that JPanel?

I have looked at the API, but I see no method for retrieving the children so that I can count them. Is this possible to count the children afterwards? Or do I have to go back and count the children as I am adding them?

By child, I mean a JComponent on which is from: myPanel.add(new JComponent()).


Solution

  • Nevermind. It was hiding in the Container API.

    myPanel.getComponentCount();