*
The moose likes Swing / AWT / SWT and the fly likes Multiple buttons... how does the actionlistener handle it? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Multiple buttons... how does the actionlistener handle it?" Watch "Multiple buttons... how does the actionlistener handle it?" New topic
Author

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

mark geroso
Greenhorn

Joined: Oct 12, 2006
Posts: 24
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

Joined: Apr 12, 2006
Posts: 175
you can use e.getActionCommang() to get the text of the button in case you have some text in them.


regards,
mark geroso
Greenhorn

Joined: Oct 12, 2006
Posts: 24
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

Joined: Jul 11, 2001
Posts: 14112
Register an individual listener per button that simply knows which button it belongs to!


The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24183
    
  34

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.


[Jess in Action][AskingGoodQuestions]
mark geroso
Greenhorn

Joined: Oct 12, 2006
Posts: 24

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

Joined: Jul 11, 2001
Posts: 15299
    
    6



GenRocket - Experts at Building Test Data
Parth Bhatt
Ranch Hand

Joined: Oct 19, 2005
Posts: 58
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 ]

If your new Big Idea doesn't scare the hell out of you, <br />it's probably not a "new Big Idea".
mark geroso
Greenhorn

Joined: Oct 12, 2006
Posts: 24
Aymane, Ilja, Ernest, Gregg and Parth, thank you for your help.
You've all given me a lot to think about.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Multiple buttons... how does the actionlistener handle it?
 
Similar Threads
why controller is servlet
how to create insert, update, delete buttons on the same page?
About Servlet
howto detect which button is click
How to press JButtons from within the program?