gumenimeda
gumenimeda

Reputation: 835

How to check what checkboxes are checked within a JFrame?

I have several checkboxes within a JForm(SubComponents) and I am using this code to check which one's are checked:

countItems = 0;
    for(Component jb: this.getComponents())
    {
        if((jb instanceof JCheckBox) ) //I never get pass this point(its always false)
        {   
            JCheckBox chbox = (JCheckBox)jb;
            if(chbox.isSelected())
            countItems++;
        }
    }

Does anyone know what the problem is?

Thank you

EDIT: Here is xml:

<Form version="" maxVersion"" type="">
   <AuxValues>
   </AuxValues>
   <Layout>
   </Layout>
   <SubComponents>
     <Component class="javax.swing.JCheckBox" name="jCheckBox1">
       <Properties>
         <Property name="text" type="java.lang.String" value="Cheese"/>
       </Properties>
     </Component>
     <Component class="javax.swing.JCheckBox" name="jCheckBox2">
       <Properties>
         <Property name="text" type="java.lang.String" value="Sausage"/>
       </Properties>
     </Component>
     <Component class="javax.swing.JCheckBox" name="jCheckBox3">
       <Properties>
         <Property name="text" type="java.lang.String" value="Pepperoni"/>
       </Properties>
     </Component>
     <Component class="javax.swing.JCheckBox" name="jCheckBox4">
       <Properties>
         <Property name="text" type="java.lang.String" value="Mushroom"/>
       </Properties>
     </Component>
  </SubComponents>
</Form>

Upvotes: 1

Views: 2512

Answers (3)

aleroot
aleroot

Reputation: 72636

Maybe your checkboxes are inside another container such as JPanel in that case you should call getcomponents on the container components, otherwise you could create a method that you call recursively when you find another container components inside the jframe, so you can traverse all hierarchy ...

Upvotes: 3

Hauke Ingmar Schmidt
Hauke Ingmar Schmidt

Reputation: 11607

As you don't show the creation of the form (what is JForm, btw?) my best guess is that the checkboxes are not children of this but some panel or other container that itself is a child of this. If you want to follow you approach of iterating over all components you need to do it recursively, i.e. if (jb instancof Container) <iterate over children>.

But this approach is not so well. You need to either remember the checkboxes so you can easily query their state or you should use models, in the case of JCheckBox it would be ButtonModel which could alter the state of a fitting data structure that holds your relevant data.

Upvotes: 3

Hovercraft Full Of Eels
Hovercraft Full Of Eels

Reputation: 285403

What is "this"? The JFrame? If so, you understand that the only component directly on the JFrame that you'll obtain with that search is the contentPane, and that your JCheckBoxes are likely deeper in the component hierarchy than where you're looking.

Why not make it easier on yourself and put the JCheckBoxes in a collection such as an ArrayList. Or if few in number, use their variable name.

Upvotes: 4

Related Questions