943,831 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Unsolved
  • Views: 2105
  • Java RSS
You are currently viewing page 1 of this multi-page discussion thread
Mar 20th, 2007
0

EventListener for sequence of events

Expand Post »
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.
<the values are text on the buttons>

How can I achieve this?
Reputation Points: 10
Solved Threads: 1
Newbie Poster
taineechan is offline Offline
17 posts
since Oct 2006
Mar 20th, 2007
0

Re: EventListener for sequence of events

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.
Reputation Points: 254
Solved Threads: 74
Practically a Posting Shark
thekashyap is offline Offline
804 posts
since Feb 2007
Mar 20th, 2007
0

Re: EventListener for sequence of events

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.
Moderator
Reputation Points: 1471
Solved Threads: 490
Industrious Poster
masijade is offline Offline
4,043 posts
since Feb 2006
Mar 20th, 2007
0

Re: EventListener for sequence of events

Click to Expand / Collapse  Quote originally posted by masijade ...
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.
Reputation Points: 254
Solved Threads: 74
Practically a Posting Shark
thekashyap is offline Offline
804 posts
since Feb 2007
Mar 20th, 2007
0

Re: EventListener for sequence of events

Click to Expand / Collapse  Quote originally posted by thekashyap ...
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.
Moderator
Reputation Points: 1471
Solved Threads: 490
Industrious Poster
masijade is offline Offline
4,043 posts
since Feb 2006
Mar 20th, 2007
0

Re: EventListener for sequence of events

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
Reputation Points: 10
Solved Threads: 1
Newbie Poster
taineechan is offline Offline
17 posts
since Oct 2006
Mar 20th, 2007
0

Re: EventListener for sequence of events

Now that I have one eventlistener, how do I know which button is pressed?
Is there something like an isClicked method?
Reputation Points: 10
Solved Threads: 1
Newbie Poster
taineechan is offline Offline
17 posts
since Oct 2006
Mar 20th, 2007
0

Re: EventListener for sequence of events

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.

Click to Expand / Collapse  Quote originally posted by taineechan ...
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. .
Reputation Points: 254
Solved Threads: 74
Practically a Posting Shark
thekashyap is offline Offline
804 posts
since Feb 2007
Mar 20th, 2007
0

Re: EventListener for sequence of events

My class implements ActionListener and then I have:
Java Syntax (Toggle Plain Text)
  1. public void actionPerformed(ActionEvent event)
  2. JButton button = (JButton)event.getSource();

I have tried Singleton. In C++ though..
Reputation Points: 10
Solved Threads: 1
Newbie Poster
taineechan is offline Offline
17 posts
since Oct 2006
Mar 21st, 2007
0

Re: EventListener for sequence of events

Seems like button.isSelected() will do teh job for you..
Reputation Points: 254
Solved Threads: 74
Practically a Posting Shark
thekashyap is offline Offline
804 posts
since Feb 2007

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Java Forum Timeline: JOptionPane losing focus
Next Thread in Java Forum Timeline: Problem with Deployment Descriptor





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC