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?

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.

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.

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.

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.

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:)

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

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. :).

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..

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

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

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.