I've written a MnemonicsBuilder class for JLabels and AbstractButtons. I would like to write a convenience method setMnemonics( JFrame f )
that will iterate through every child of the JFrame and select out the JLabels and AbstractButtons. How can I obtain access to everything contained in the JFrame? I've tried:
LinkedList<JLabel> harvestJLabels( Container c, LinkedList<JLabel> l ) {
Component[] components = c.getComponents();
for( Component com : components )
{
if( com instanceof JLabel )
{
l.add( (JLabel) com );
} else if( com instanceof Container )
{
l.addAll( harvestJLabels( (Container) com, l ) );
}
}
return l;
}
In some situations, this works just fine. In others, it runs out of memory. What am I not thinking of? Is there a better way to search for child components? Is my recursion flawed? Is this not a picture of how things "Contain" other things in Swing - e.g., is Swing not a Rooted Tree?
JFrame
|
|\__JMenuBar
| |
| \__JMenu
| |
| \__JMenuItem
|
|\__JPanel
| |
| |\__JButton
| |
| |\__JLabel
| |
| |\__ ... JCheckBoxes, other AbstractButtons, etc.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…