Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Multiple buttons... how does the actionlistener handle it?

 
mark geroso
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm supposed to make a basic memory game. Buttons will appear on a grid layout in variations of 2x2, 4x4 and 6x6.
How do I know which button was pressed?

Those are the buttons. I was planning on comparing the strings sent by the buttons pressed to see if they were similar or not, and work from there.
But how do I even know which button was clicked...
Any help would be greatly appreciated.
Thanks.
 
aymane chetibi
Ranch Hand
Posts: 175
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
you can use e.getActionCommang() to get the text of the button in case you have some text in them.


regards,
 
mark geroso
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is how my actionperformed looks like right now.

My problem is, how to access the button that was clicked. Like, when the button is clicked, I'd want to change the foreground color to white, wait for the second button to be clicked, compare the values and work from there...
I can't find any way to find out or manipulate whichever button was clicked...
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Register an individual listener per button that simply knows which button it belongs to!
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24208
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by mark geroso:

I can't find any way to find out or manipulate whichever button was clicked...


Call getSource() on the event object; then ask

if (e.getSource() == buttons[3]) ...

But I have to admit that Ilja's suggestion is the best. You'll have more objects, and more small classes, but the code itself will be simpler.
 
mark geroso
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Register an individual listener per button that simply knows which button it belongs to!


Can you show me a sample?
Thanks.
 
Gregg Bolinger
GenRocket Founder
Ranch Hand
Posts: 15302
6
Chrome IntelliJ IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
Parth Bhatt
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If Registering an individual listener per button is not wht you wish to do then This may help :

while creating the matrix of buttons, use the method setActionCommand(String) to set a command for each button in the 2x2, 4x4 or 6x6 matrix. Also, add the same actionListener to each button.

Then, when any of the buttons is clicked, check in the actionPerformed method for the ActionCommand string of that button using the method getActionCommand().

i.e. At the time of initializing the buttons



And in the actionPerformed method check for the command of he button that was clicked.


[ October 25, 2006: Message edited by: Parth Bhatt ]
 
mark geroso
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Aymane, Ilja, Ernest, Gregg and Parth, thank you for your help.
You've all given me a lot to think about.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic