• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 262
  • Last Modified:

Getting all checkboxes

This method right now selects all checkboxes:

    public void checkAll() {
        List list = new ArrayList();
        Component temp;
        Container container = null;
       
        int count = getComponentCount();
       
        do {
            if (list.size() > 0) {
                container = (Container)list.remove(0);
                count = container.getComponentCount();
            }
           
            for (int i = 0; i < count; i++) {
                if (container != null) {
                    temp = container.getComponent(i);
                }
                else {
                    temp = getComponent(i);
                }
                if (temp instanceof JCheckBox) {
                    ((JCheckBox) temp).setSelected(true);
                }
                else if (temp instanceof Container) {
                    list.add(temp);
                    continue;
                }                    
            }
        }
        while (list.size() > 0);
    }

I got it from somewhere else, I'm looking for two things:

First off, I have a rough idea of what it's doing, but I don't know for sure, so if someone could explain how it does what it does. I assume that it gets all the checkboxes in the main container and checks them, but I have some check boxes outside of the ones I am trying to check that still get checked.....

Secondly,

I need it to only select the checkboxes in a certain selected JTabbedPane and not the whole application.

Is that possible?

Thanks!
0
k41d3n
Asked:
k41d3n
  • 5
  • 2
  • 2
1 Solution
 
riaancorneliusCommented:
public void setCheckbox( java.awt.Container comp ){
        int count = comp.getComponentCount();
        for ( int i = 0; i<count; i++ ){
            if ( comp.getComponent(i) instanceof javax.swing.JPanel){
                setCheckBox( (java.awt.Container)comp.getComponent(i) );
            }else if ( comp.getComponent(i) instanceof javax.swing.JCheckBox){
                javax.swing.JTextArea tmp = (javax.swing.JTextArea)comp.getComponent(i);
                tmp.setSelected(true);
            }
        }
    }
0
 
riaancorneliusCommented:
just pass whatever container to this, and it will set all checkboxes on that container. If the container is a JPanel, it just runs recursively for that panel, setting the checkboxes on the panel.
0
 
CEHJCommented:
The code is rather baroque but basically scans the current Container and its subcontainers for checkboxes. You will get more control by simply adding your checkboxes on each pane to an array or list. You can then simply iterate it to set/get
0
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
CEHJCommented:
/*( JCheckBox[] ) */ boxes = new JCheckBox[NUMBER_OF_CHECKBOXES];
boxes[0] = new JCheckBox("X"); //etc

// Now you can iterate them later:

for(int i = 0;i < NUMBER_OF_CHECKBOXES;i++) {
      if (boxes[i].getSelected()) {
            // It's selected
      }
 
}
0
 
CEHJCommented:
Or in your case:


for(int i = 0;i < NUMBER_OF_CHECKBOXES;i++) {
    boxes[i].setSelected();
}
0
 
k41d3nAuthor Commented:
CEHJ,

I like your idea, but I'm wondering where NUMBER_OF_CHECKBOXES comes from?
0
 
CEHJCommented:
You define that as a constant. If it's dynamic then create a List instead and do cbList.size() to find the value
0
 
k41d3nAuthor Commented:
LOooks good bud, thank you.

Thanks riaancornelius for trying as well.
0
 
CEHJCommented:
:-)
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: Certified Penetration Testing

This CPTE Certified Penetration Testing Engineer course covers everything you need to know about becoming a Certified Penetration Testing Engineer. Career Path: Professional roles include Ethical Hackers, Security Consultants, System Administrators, and Chief Security Officers.

  • 5
  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now