Results 1 to 9 of 9

Thread: JButton Array & ActionListener: HELP?

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Jan 2002
    Location
    Burlington, ON, Canada
    Posts
    99

    JButton Array & ActionListener: HELP?

    I have a 6 X 6 grid of JButtons, layed out with this code:

    PHP Code:
    btnGrid = new JButton[6][6];
          for (
    int i=0i<6i++) {
             for (
    int j=0j<6j++) {
                
    btnGrid[i][j] = new JButton();
                
    btnGrid[i][j].addActionListener(this);         
                
    pCentre.add(btnGrid[i][j]);
             }
          } 
    I want to know if the ActionListener can pickup when any button in the array is clicked rather then just a specific button

    Thanks....

  2. #2
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    The ActionEvent argument to your handler function contains a sender member which is the button that fired the event. This is how you can distinguish.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Jan 2002
    Location
    Burlington, ON, Canada
    Posts
    99
    Yes I understand that...

    however I need something like this...

    PHP Code:
    if (event.getSource() == btnGrid) { 
             if (
    event.getSource() == btnGrid[iRand1][iRand2]) {
                
    iHits++;
                
    lblHits.setText("Hits = " Integer.toString(iHits));
                
    passfail();
                
    btnGrid[iRand1][iRand2].setEnabled(false); 
             }                     
             else {
                
    iMisses++;
                
    lblMisses.setText("Misses = " Integer.toString(iMisses));
             }
          } 
    Thanks

  4. #4
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    Loop through the grid and compare to each.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  5. #5

    Thread Starter
    Lively Member
    Join Date
    Jan 2002
    Location
    Burlington, ON, Canada
    Posts
    99
    Originally posted by CornedBee
    Loop through the grid and compare to each.
    code snippet please??

    Code:
    if (event.getSource() == btnGrid)
    that code won't pick up any events....

  6. #6
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    It's the slow way...
    Code:
    Object snd = e.getSender();
    for(int i = 0; i < btnGrid.length; ++i) {
      for(int j = 0; j < btnGrid[i].length; ++j) {
        if(snd == btnGrid[i][j]) {
          // found a match, you can break the loop
        }
      }
    }
    But I believe components have some user data field that you can set. It would be far easier to simply write a tiny class Coord with just two public int members which tell you the coordinates in the btnArray. When creating the buttons, you create such a struct for each and set it as user data. This is much faster because you don't have to loop through each control.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  7. #7

    Thread Starter
    Lively Member
    Join Date
    Jan 2002
    Location
    Burlington, ON, Canada
    Posts
    99
    i understand all that....but i need to do commands if any other button is pressed as well

    if any buttonpress
    if specific button
    do this
    else //other button
    do this


    thanks

  8. #8
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    Assign a different handler. Nested anonymous classes are your friend there.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  9. #9

    Thread Starter
    Lively Member
    Join Date
    Jan 2002
    Location
    Burlington, ON, Canada
    Posts
    99
    thanks for the help...i did exactly that

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