0

I am not sure how to ask this. The program I am working on is done, but it seems like it has excessive code. Here is part of the code:

chkDef1 = new JCheckBox

if (chkDef1.isSelected()) {
            actual = chkDef1.getText();
        } 
else if (chkDef2.isSelected()) {
            actual = chkDef2.getText();
        } 
else if (chkDef3.isSelected()) {
            actual = chkDef3.getText();
        } 
else {
            actual = chkDef4.getText();
        }

There are other areas where there is a lot of duplicate code with the chkDef1 - 4 checkboxes. What I would like to do is use a loop in the areas where the code is duplicated and then just use 1 assignment statement.

I've tried : if(('chkDef' + counter).isSelected())

I've also tried assigning "'chkDef' + counter" to a String variable and then adding isSelected. Unfortunately I keep getting error messages.

I am a novice programmer so I do not know if what I want to do is possible or what it is called. If it is possible an explanation of how would be appreciated.

mKorbel
  • 109,525
  • 20
  • 134
  • 319
user1793408
  • 113
  • 11

2 Answers2

2

Simply create a list of checkboxes and iterate through it.

ArrayList<JCheckBox> checkboxes  = new ArrayList<JCheckBox>();
//Init your checkboxes array. 

for(JCheckbox chkbox :checkboxes)
{
  if(chkbox.isSelected())
   {
    actual = chkbox.getText() ; break;
  }
}

Although, there could be a JCheckbox group that does what you want.

Looks like you can use ButtonGroup and get the elements to iterate through it.

Lews Therin
  • 10,907
  • 4
  • 48
  • 72
  • +1 for the button group (although I'd recommend `JRadioButton` or `JToggledButton` for button groups from a useability perspective) – MadProgrammer Nov 28 '12 at 00:34
  • Thanks for the help. I do have them as part of a button group. I'll start with the array as I understand how that works and then work on using the button group. – user1793408 Nov 28 '12 at 00:39
2

You could create an array that contains all the check boxes and then loop through the array...

JCheckBox[] boxes = new JCheckBox[] {chkDef1,chkDef2,chkDef3,chkDef4}
for (JCheckBox box : boxes) {
    if (box.isSelected()) {
        actual = box.getText();
        break; // We don't want to loop unnecessarily
    }
}

Equally, you could create a simple method that takes a variable number of arguments...

public String getCheckedItem(JCheckBox... boxes) {
    String actual = null;
    for (JCheckBox box : boxes) {
        if (box.isSelected()) {
            actual = box.getText();
            break; // We don't want to loop unnecessarily
        }
    }
    return actual;
}

And call it like...

String actual = getCheckItem(chkDef1, chkDef2, chkDef3, chkDef4);

Personally, I'd return the check box, but that's up to you

If you're only interested in maintaining a single selected check box (ie not allowing multiple check boxes to be selected) then you should seriously consider using JRadioButtons and ButtonGroup instead.

Otherwise you could collect ALL the selected checked boxes...

public JCheckBox[] getCheckedItem(JCheckBox... boxes) {
    List<JCheckBox> selected = new ArrayList<JCheckBox>(boxes.length);
    for (JCheckBox box : boxes) {
        if (box.isSelected()) {
            selected.add(box);
        }
    }
    return selected.toArray(new JCheckBox[selected.size]);
}
MadProgrammer
  • 343,457
  • 22
  • 230
  • 366