Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 Question regarding arrays of JButton and ActionListeners...
Index -> Programming, Java -> Java Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
Regeane




PostPosted: Sun May 28, 2006 8:25 am   Post subject: Question regarding arrays of JButton and ActionListeners...

I'm creating a BINGO program, and it has a 25X25 grid of buttons. I have it set up as a 3D array of JButton, and I have each button respond when it's clicked, but I need some way to register which button has been clicked, and I was wondering if there's a better way to do it than lots and lots of if statements...

This is the code I have for drawing the cards:
code:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class BingoDrawer{
        JFrame[] frame;
        JPanel[] pane;
        JButton[][] button;
        int x,y,z;
        public BingoDrawer(int num_players, BingoGame bg){
                frame = new JFrame[num_players];
                pane = new JPanel[num_players];
                button = new JButton[num_players][25];
                for(x=0;x<num_players;x++){
                        int b=0,i=0,n=0,g=0,o=0;
                        char col_letter='X';
                        frame[x]=new JFrame("Player "+(x+1));
                        frame[x].setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                        frame[x].setSize(500,500);
                        pane[x] = (JPanel) frame[x].getContentPane();
                        pane[x].setLayout(new GridLayout(5,5));
                        for(y=0;y<25;y++){
                                int temp=(y%5)+1;
                                switch(temp){
                                        case 1: pane[x].add(button[x][y]=new JButton("B"+(bg.bgc[x].bc[b][y%5])));
                                                        button[x][y].addActionListener(new BttnListener(button[x][y]));
                                                        b++;break;
                                        case 2: pane[x].add(button[x][y]=new JButton("I"+(bg.bgc[x].bc[i][y%5])));
                                                        button[x][y].addActionListener(new BttnListener(button[x][y]));
                                                        i++;break;
                                        case 3: if(n==2){
                                                                pane[x].add(button[x][y]=new JButton("FREE"));
                                                        }
                                                        else{
                                                                pane[x].add(button[x][y]=new JButton("N"+(bg.bgc[x].bc[n][y%5])));
                                                        }
                                                        button[x][y].addActionListener(new BttnListener(button[x][y]));
                                                        n++;break;
                                        case 4: pane[x].add(button[x][y]=new JButton("G"+(bg.bgc[x].bc[g][y%5])));
                                                        button[x][y].addActionListener(new BttnListener(button[x][y]));
                                                        g++;break;
                                        case 5: pane[x].add(button[x][y]=new JButton("O"+(bg.bgc[x].bc[o][y%5])));
                                                        button[x][y].addActionListener(new BttnListener(button[x][y]));
                                                        o++;break;

                                }
                        }
                        frame[x].setVisible(true);
                }
        }
}
class BttnListener implements ActionListener{
        JButton source;
       
        BttnListener(JButton bttn){
                source=bttn;
        }
       
        public void actionPerformed(ActionEvent e){
                source.setLabel("aie");
        }
}


Any help would be greatly appreciated.
Sponsor
Sponsor
Sponsor
sponsor
wtd




PostPosted: Sun May 28, 2006 9:37 am   Post subject: (No subject)

Subclass JButton. Have this subclass implement ActionListener.

Make sure the subclass knows where it's located in the grid.
Regeane




PostPosted: Sun May 28, 2006 12:41 pm   Post subject: (No subject)

Pardon my incompetance, but... *still confused*
wtd




PostPosted: Sun May 28, 2006 1:02 pm   Post subject: (No subject)

Well, first thing's first. Do you know what I mean by "subclass JButton"?
Regeane




PostPosted: Sun May 28, 2006 1:04 pm   Post subject: (No subject)

'Fraid not... I *sortof* understand subclass, but not really.
wtd




PostPosted: Sun May 28, 2006 1:40 pm   Post subject: (No subject)

You might benefit from reading my Introduction to Java in the Java tutorials forum. It covers this, as well as a number of other important concepts.
Regeane




PostPosted: Sun May 28, 2006 3:01 pm   Post subject: (No subject)

Ok, well I read the whole tutorial, and I'm still not sure what you mean by 'subclass JButton.' Is JButton an interface, or...

*feels daft*


[And, while I'm thinking of it, having little to do with my original question, more to do with the content of your tutorial: with the throwing and catching of exceptions, where does the try come in?]
wtd




PostPosted: Sun May 28, 2006 3:07 pm   Post subject: (No subject)

Regeane wrote:
Ok, well I read the whole tutorial, and I'm still not sure what you mean by 'subclass JButton.' Is JButton an interface, or...

*feels daft*


Google "site:java.sun.com JButton". Smile

Regeane wrote:
[And, while I'm thinking of it, having little to do with my original question, more to do with the content of your tutorial: with the throwing and catching of exceptions, where does the try come in?]


"try" does just what it sounds like. It attempts to run a block of code. If any exception should happen to be thrown, then it falls to the attached "catch" blocks to deal with that exception.
Sponsor
Sponsor
Sponsor
sponsor
Regeane




PostPosted: Sun May 28, 2006 3:16 pm   Post subject: (No subject)

wtd wrote:
"try" does just what it sounds like. It attempts to run a block of code. If any exception should happen to be thrown, then it falls to the attached "catch" blocks to deal with that exception.
code:


Err.... no code there...
wtd




PostPosted: Sun May 28, 2006 4:09 pm   Post subject: (No subject)

Weird.

Oh well. Fixed.
Regeane




PostPosted: Sun May 28, 2006 10:01 pm   Post subject: (No subject)

Ok, I seem to have gotten past the initial brainloss... figured out the 'subclass JButton', but one more question... when you say "Make sure the subclass knows where it's located in the grid", is that to imply that I'd be using the *subclass* of JButton instead of JButton?
wtd




PostPosted: Sun May 28, 2006 10:10 pm   Post subject: (No subject)

Well, there'd be no point in creating a subclass you don't use. Wink

And basically that means your MyFunnilyNamedButton objects must accept that information via their constructor and store that information somehow.
Regeane




PostPosted: Mon May 29, 2006 3:40 pm   Post subject: (No subject)

I figured it out, thanks so so so much for your help!!
wtd




PostPosted: Mon May 29, 2006 3:45 pm   Post subject: (No subject)

Glad I could help in my roundabout way. Smile
Display posts from previous:   
   Index -> Programming, Java -> Java Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 14 Posts ]
Jump to:   


Style:  
Search: