?
Solved

Getting all checkboxes

Posted on 2005-05-17
9
Medium Priority
?
260 Views
Last Modified: 2010-03-31
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
Comment
Question by:k41d3n
  • 5
  • 2
  • 2
9 Comments
 
LVL 9

Expert Comment

by:riaancornelius
ID: 14020845
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
 
LVL 9

Expert Comment

by:riaancornelius
ID: 14020866
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
 
LVL 86

Expert Comment

by:CEHJ
ID: 14020867
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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 86

Expert Comment

by:CEHJ
ID: 14020903
/*( 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
 
LVL 86

Expert Comment

by:CEHJ
ID: 14020915
Or in your case:


for(int i = 0;i < NUMBER_OF_CHECKBOXES;i++) {
    boxes[i].setSelected();
}
0
 
LVL 2

Author Comment

by:k41d3n
ID: 14021117
CEHJ,

I like your idea, but I'm wondering where NUMBER_OF_CHECKBOXES comes from?
0
 
LVL 86

Accepted Solution

by:
CEHJ earned 2000 total points
ID: 14021185
You define that as a constant. If it's dynamic then create a List instead and do cbList.size() to find the value
0
 
LVL 2

Author Comment

by:k41d3n
ID: 14021817
LOooks good bud, thank you.

Thanks riaancornelius for trying as well.
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 14021828
:-)
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
Suggested Courses
Course of the Month16 days, 19 hours left to enroll

864 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question