Skip to Main Content

Java Programming

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

JPanel components access by name

807588Oct 24 2008 — edited Feb 15 2009
I have JFrame with few JPanels on it. Each of JPanels has some components(JTextField, JCheckBox,...) or other JPanels on it, and so on...
I am trying to change property of one particular JCheckBox (namely, set it disabled) which is placed somewhere on form (on one of panels or one of it's child panels...).
I tried to play with reflection, but I didn't go far...
I made a method to iterate over all components, but I don't know how to compare component's name with checkbox name (chbAvailable).
When I try component.getName() I get null.
    public static boolean getComponent(JComponent parent, String wantedComponentName){
        try  {
            Component[] components = parent.getComponents();
            for(int i = 0; i < components.length; i++){
                if(components.getName().equals(wantedComponentName)){
components[i].setEnabled(false);
return true;
}
if(components[i] instanceof JPanel ||
components[i] instanceof JAPanel){
if(getComponent((JComponent)components[i], wantedComponentName)){
return true;
}
}
}
} catch (Exception ex) {
ex.printStackTrace();
}
return false;
}
Can someone please help me how can I (while iterating over components) get component name to compare it to checkbox name chbAvailable?                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Mar 15 2009
Added on Oct 24 2008
13 comments
871 views