k41d3n
asked on
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.getComponentCoun t();
}
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!
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.getComponentCoun
}
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!
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.
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
/*( JCheckBox[] ) */ boxes = new JCheckBox[NUMBER_OF_CHECKB OXES];
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
}
}
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
}
}
Or in your case:
for(int i = 0;i < NUMBER_OF_CHECKBOXES;i++) {
boxes[i].setSelected();
}
for(int i = 0;i < NUMBER_OF_CHECKBOXES;i++) {
boxes[i].setSelected();
}
ASKER
CEHJ,
I like your idea, but I'm wondering where NUMBER_OF_CHECKBOXES comes from?
I like your idea, but I'm wondering where NUMBER_OF_CHECKBOXES comes from?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
LOooks good bud, thank you.
Thanks riaancornelius for trying as well.
Thanks riaancornelius for trying as well.
:-)
int count = comp.getComponentCount();
for ( int i = 0; i<count; i++ ){
if ( comp.getComponent(i) instanceof javax.swing.JPanel){
setCheckBox( (java.awt.Container)comp.g
}else if ( comp.getComponent(i) instanceof javax.swing.JCheckBox){
javax.swing.JTextArea tmp = (javax.swing.JTextArea)com
tmp.setSelected(true);
}
}
}