CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    Nov 2002
    Location
    France
    Posts
    70

    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

  2. #2
    dlorde is offline Elite Member Power Poster
    Join Date
    Aug 1999
    Location
    UK
    Posts
    10,163
    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 &#91;CODE]...your code here...&#91;/CODE] tags when posting code. If you get an error, please post the full error message and stack trace, if present.

  3. #3
    Join Date
    Nov 2002
    Location
    France
    Posts
    70
    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
  •  





Click Here to Expand Forum to Full Width

Featured