andrew Patterson
andrew Patterson

Reputation: 579

JSpinner Source?

I have a class with two JSpinner objects in them, x and y. I have one change Listener which is added to both. can someone tell me how I can implement my change listener so that the listener can tell the difference between the two objects. e.g. Pseudocode:

if(source equals x)
    do this
else if(source equals y)
    do that

Thanks guys,

Upvotes: 0

Views: 108

Answers (2)

MadProgrammer
MadProgrammer

Reputation: 347234

It's more prudent (as Ali has pointed out, +1) to use a single listener per control where possible. It isolates the event/action and makes it generally easier to read and make sense of...

If you can't see yourself using this, then every EventObject has a getSource method which is a reference to the control which raised the event...

public void stateChanged(ChangeEvent e)
{
    if (e.getSource() == xControl) {
        // xControl updated
    } else if (e.getSource() == yControl) {
        // yControl updated
    }
}

Upvotes: 2

Kakalokia
Kakalokia

Reputation: 3191

You can simply use an anonymous class to implement the listener for each spinner

For example if you want to implement change listener to x, you can do something like:

x.addChangeListener(new ChangeListener()
{
   public void stateChanged(ChangeEvent e)
   {
   }
});

and same thing for y

Upvotes: 3

Related Questions