| | |
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.
<the values are text on the buttons>
How can I achieve this?
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?
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.
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.
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
----------------------------------------------
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
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.
•
•
•
•
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.
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
----------------------------------------------
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
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.
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.
.
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.
. My class implements ActionListener and then I have:
I have tried Singleton. In C++ though..
Java Syntax (Toggle Plain Text)
public void actionPerformed(ActionEvent event) JButton button = (JButton)event.getSource();
I have tried Singleton. In C++ though..
![]() |
Other Threads in the Java Forum
- Previous Thread: JOptionPane losing focus
- Next Thread: i want problem for practic.
| Thread Tools | Search this Thread |
account android api applet application array arrays automation awt bidirectional binary birt bluetooth busy_handler(null) chat class classes client code columns component database designadrawingapplicationusingjavajslider draw eclipse error errors exception expand fractal game givemetehcodez graphics gui guidancer homework html ide image inetaddress inheritance input integer intellij j2me java javamicroeditionuseofmotionsensor javaprojects jlabel jme jni jpanel jtextfield julia linux list loop map method methods midlethttpconnection mobile mobiledevelopmentcreatejar monitoring myaggfun netbeans newbie nullpointerexception open-source plazmic print problem program programming project property recursion ria scanner search server set smart sms smsspam sort sourcelabs splash sql sqlite static string subclass support swing testautomation threads tree webservices windows






