954,124 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

EventListener for sequence of events

Hi
I'm coding a game of towers in java. I'm having trouble moving a stack from one button to another.

I have eventlisteners for buttons A and B. If Button A is clicked and then button B is clicked, the value at button A must be added to the value at button B.


How can I achieve this?

taineechan
Newbie Poster
17 posts since Oct 2006
Reputation Points: 10
Solved Threads: 1
 

Well you've gotta have some common place to store the values of these buttons..
So may be have a class (say my_container) to contain these values. Catch is both the button's handler classes must have access to same object of this my_container class. You can do this is with Singleton patten .

thekashyap
Practically a Posting Shark
811 posts since Feb 2007
Reputation Points: 254
Solved Threads: 75
 

Well, if the instances of the Buttons themselves are known to the ActionListener (i.e. they are instance variables that the ActionListener has access to) you can simply use getText() to get the displayed value on the button.

masijade
Industrious Poster
Moderator
4,253 posts since Feb 2006
Reputation Points: 1,471
Solved Threads: 494
 
Well, if the instances of the Buttons themselves are known to the ActionListener (i.e. they are instance variables that the ActionListener has access to) you can simply use getText() to get the displayed value on the button.


Is it not that ActionListners for both buttons would be different objects? If so you'll run into same problem. (both ActionListner objects have to know both Button objects). Instead on click of A, update the value in "common place" and on click of B pickup the value from "common place" and do the needful.

thekashyap
Practically a Posting Shark
811 posts since Feb 2007
Reputation Points: 254
Solved Threads: 75
 
Is it not that ActionListners for both buttons would be different objects? If so you'll run into same problem. (both ActionListner objects have to know both Button objects). Instead on click of A, update the value in "common place" and on click of B pickup the value from "common place" and do the needful.

It does not need to be different ActionListeners for each button. That normally only happens if you use anonymous inner classes for the ActionListeners, but you don't have to do that. And, as a matter of fact, since the action of each buton depends on a possible previous action of another button, you should use the same ActionListener for both buttons and differentiate the action according to which button was pressed and whaqt action was previously done by using an if statement and a state variable.

masijade
Industrious Poster
Moderator
4,253 posts since Feb 2006
Reputation Points: 1,471
Solved Threads: 494
 

I've put all my buttons into a 2D array and they use one listener. I'm gonna try the if statement and previous variable thing.

I'll only try Singleton if that doesn't work coz I personally find design patterns a little scary.

Thanks so much for the help:)

taineechan
Newbie Poster
17 posts since Oct 2006
Reputation Points: 10
Solved Threads: 1
 

Now that I have one eventlistener, how do I know which button is pressed?
Is there something like an isClicked method?

taineechan
Newbie Poster
17 posts since Oct 2006
Reputation Points: 10
Solved Threads: 1
 

What's the type of listner you're using? (Class name)
If you're using/registering a single listner object for both buttons' click event, you can do what masijade said.

I'll only try Singleton if that doesn't work coz I personally find design patterns a little scary.


They are not scary. And you've GOT TO try singleton (if not for this problem then something else) if you wanna be in this field. :).

thekashyap
Practically a Posting Shark
811 posts since Feb 2007
Reputation Points: 254
Solved Threads: 75
 

My class implements ActionListener and then I have:

public void actionPerformed(ActionEvent event)
JButton button = (JButton)event.getSource();


I have tried Singleton. In C++ though..

taineechan
Newbie Poster
17 posts since Oct 2006
Reputation Points: 10
Solved Threads: 1
 

Seems like button.isSelected() will do teh job for you..

thekashyap
Practically a Posting Shark
811 posts since Feb 2007
Reputation Points: 254
Solved Threads: 75
 

Thanks so much for all the help..
It's working now:)

taineechan
Newbie Poster
17 posts since Oct 2006
Reputation Points: 10
Solved Threads: 1
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You