EventListener for sequence of events

Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Oct 2006
Posts: 17
Reputation: taineechan is an unknown quantity at this point 
Solved Threads: 1
taineechan's Avatar
taineechan taineechan is offline Offline
Newbie Poster

EventListener for sequence of events

 
0
  #1
Mar 20th, 2007
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?
Reply With Quote Quick reply to this message  
Join Date: Feb 2007
Posts: 539
Reputation: thekashyap will become famous soon enough thekashyap will become famous soon enough 
Solved Threads: 50
thekashyap's Avatar
thekashyap thekashyap is offline Offline
Posting Pro

Re: EventListener for sequence of events

 
0
  #2
Mar 20th, 2007
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.
Reply With Quote Quick reply to this message  
Join Date: Feb 2006
Posts: 2,450
Reputation: masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of 
Solved Threads: 262
Moderator
masijade's Avatar
masijade masijade is offline Offline
Nearly a Posting Maven

Re: EventListener for sequence of events

 
0
  #3
Mar 20th, 2007
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.
Java Programmer and Sun Systems Administrator

----------------------------------------------

Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.
--Brian Kernighan
Reply With Quote Quick reply to this message  
Join Date: Feb 2007
Posts: 539
Reputation: thekashyap will become famous soon enough thekashyap will become famous soon enough 
Solved Threads: 50
thekashyap's Avatar
thekashyap thekashyap is offline Offline
Posting Pro

Re: EventListener for sequence of events

 
0
  #4
Mar 20th, 2007
Originally Posted by masijade View Post
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.
Reply With Quote Quick reply to this message  
Join Date: Feb 2006
Posts: 2,450
Reputation: masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of 
Solved Threads: 262
Moderator
masijade's Avatar
masijade masijade is offline Offline
Nearly a Posting Maven

Re: EventListener for sequence of events

 
0
  #5
Mar 20th, 2007
Originally Posted by thekashyap View Post
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.
Java Programmer and Sun Systems Administrator

----------------------------------------------

Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.
--Brian Kernighan
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 17
Reputation: taineechan is an unknown quantity at this point 
Solved Threads: 1
taineechan's Avatar
taineechan taineechan is offline Offline
Newbie Poster

Re: EventListener for sequence of events

 
0
  #6
Mar 20th, 2007
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
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 17
Reputation: taineechan is an unknown quantity at this point 
Solved Threads: 1
taineechan's Avatar
taineechan taineechan is offline Offline
Newbie Poster

Re: EventListener for sequence of events

 
0
  #7
Mar 20th, 2007
Now that I have one eventlistener, how do I know which button is pressed?
Is there something like an isClicked method?
Reply With Quote Quick reply to this message  
Join Date: Feb 2007
Posts: 539
Reputation: thekashyap will become famous soon enough thekashyap will become famous soon enough 
Solved Threads: 50
thekashyap's Avatar
thekashyap thekashyap is offline Offline
Posting Pro

Re: EventListener for sequence of events

 
0
  #8
Mar 20th, 2007
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.

Originally Posted by taineechan View Post
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. .
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 17
Reputation: taineechan is an unknown quantity at this point 
Solved Threads: 1
taineechan's Avatar
taineechan taineechan is offline Offline
Newbie Poster

Re: EventListener for sequence of events

 
0
  #9
Mar 20th, 2007
My class implements ActionListener and then I have:
  1. public void actionPerformed(ActionEvent event)
  2. JButton button = (JButton)event.getSource();

I have tried Singleton. In C++ though..
Reply With Quote Quick reply to this message  
Join Date: Feb 2007
Posts: 539
Reputation: thekashyap will become famous soon enough thekashyap will become famous soon enough 
Solved Threads: 50
thekashyap's Avatar
thekashyap thekashyap is offline Offline
Posting Pro

Re: EventListener for sequence of events

 
0
  #10
Mar 21st, 2007
Seems like button.isSelected() will do teh job for you..
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Other Threads in the Java Forum
Thread Tools Search this Thread



Tag cloud for Java
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC