|
-
Feb 1st, 2004, 12:23 AM
#1
Thread Starter
Lively Member
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=0; i<6; i++) {
for (int j=0; j<6; j++) {
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....
-
Feb 2nd, 2004, 04:06 AM
#2
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.
-
Feb 2nd, 2004, 09:43 AM
#3
Thread Starter
Lively Member
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
-
Feb 2nd, 2004, 10:07 AM
#4
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.
-
Feb 2nd, 2004, 10:20 AM
#5
Thread Starter
Lively Member
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....
-
Feb 2nd, 2004, 01:17 PM
#6
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.
-
Feb 2nd, 2004, 02:16 PM
#7
Thread Starter
Lively Member
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
-
Feb 3rd, 2004, 04:57 AM
#8
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.
-
Feb 3rd, 2004, 09:06 AM
#9
Thread Starter
Lively Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|