richard
richard

Reputation: 605

Internal and External ChangeEvent (JSlider w/Change Listener)

As far as I can determine, if I add a ChangeListener to a JSlider component, then a ChangeEvent event is fired whether the aforementioned slider has its value changed either internally by the program or externally by the user through the GUI.

Is there a way to only have the ChangeListener recognize external changes to the JSlider's value? That is, I want to be able to internally set the value of the slider multiple times throughout the course of the program without having an event fired.

Upvotes: 0

Views: 477

Answers (2)

Reimeus
Reimeus

Reputation: 159754

You are always going to fire a ChangeEvent whether you change the JSlider value internally or externally. You could always have a class member variable to indicate from where the change is being made...

Upvotes: 1

Robin
Robin

Reputation: 36611

An option is to keep a flag in your listener which indicates whether the received event is externally triggered or not.

When you change the value, you first update the value of the flag, avoiding that your listener reacts on the changes you just made. Afterwards, you restore the state of the flag.

Upvotes: 2

Related Questions