|
-
December 21st, 2002, 01:52 PM
#1
Adding an action listener to an array of checkboxes
Hi,
In my program i have 6 arrays of checkboxes each array has 6 checkboxes
Code:
// 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
-
December 22nd, 2002, 10:11 AM
#2
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:
Code:
// 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.
Please use [CODE]...your code here...[/CODE] tags when posting code. If you get an error, please post the full error message and stack trace, if present.
-
December 22nd, 2002, 12:45 PM
#3
That is what i wanted to do, thanks again for your help Dave...
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|