Click to See Complete Forum and Search --> : Adding an action listener to an array of checkboxes


sam_ccld
December 21st, 2002, 12:52 PM
Hi,
In my program i have 6 arrays of checkboxes each array has 6 checkboxes

// declaring the arrays JCheckBox[]
pre = new JCheckBox[7];
JCheckBox[] expr = new JCheckBox[7];
JCheckBox[] mot = new JCheckBox[7];
JCheckBox[] res = new JCheckBox[7];
JCheckBox[] dyn = new JCheckBox[7];
JCheckBox[] appr = new JCheckBox[7];
// initialising the checkboxes
for (int j=0 ; j<=6 ; j++){
pre[j] = new JCheckBox("0"+j);
expr[j] = new JCheckBox("1"+j);
mot[j] = new JCheckBox("2"+j);
res[j] = new JCheckBox("3"+j);
dyn[j] = new JCheckBox("4"+j);
appr[j] = new JCheckBox("5"+j);
}
int top=170; int left=10;
for(int x=0;x<=6;x++){
jPanel2.add(pre[x], new XYConstraints(top+x*40, left, 30, 20));
jPanel2.add(expr[x], new XYConstraints(top+x*40, left+20, 30, 18));
jPanel2.add(mot[x], new XYConstraints(top+x*40, left+40, 30, 18));
jPanel2.add(res[x], new XYConstraints(top+x*40, left+60, 30, 18));
jPanel2.add(dyn[x], new XYConstraints(top+x*40, left+80, 30, 18));
jPanel2.add(appr[x], new XYConstraints(top+x*40, left+100, 28, 19));
}

I want to add an Action Listener to each array of checkboxes, and I do not know how to do it, could anyone give me an example please
Your help is appreciated, many thanks in advance

dlorde
December 22nd, 2002, 09:11 AM
If you want each array to have a separate action listener, shared by each of its check boxes, just create each action listener and set it into each check box in the appropriate array, e.g:// Create ActionListener for a checkbox array
ActionListener listener3 = new ActionListener() {
public actionPerformed(ActionEvent e) {
// do something for action events from a check box in array 3
}
};
// Set ActionListener into all checkboxes in the array
for (int i=0; i<array3.length; i++) {
array3[i].addActionListener(listener3);
}
// ...etc.If that's not quite what you wanted, you'll have to be more specific.

sam_ccld
December 22nd, 2002, 11:45 AM
That is what i wanted to do, thanks again for your help Dave...