Sounds like you'd be better counting the components in the hierarchy rather than on a single component, for example:
public static int countSubComponentsOfType(Container container, Class<? extends Component> type){
int count = 0;
for(Component component : container.getComponents()){
if(type.isInstance(component)) count++;
if(component instanceof Container)
count += countSubComponentsOfType((Container)component, type);
}
return count;
}
Alternatively, if you genuinely want the components on a JFrame, use the following instead.
frame.getRootPane().getComponentCount();
This is because JFrame always just contains 1 child component, a JRootPane. Anything added to the JFrame is added to the rootpane.