7

How can I check if the value of a JSlider or JSpinner was set via the graphical interface and not via the method setValue( int n) ?

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
andreihondrari
  • 5,743
  • 5
  • 30
  • 59

2 Answers2

5

Set a boolean value to true whenever you call setValue programmatically (before calling it), and reset it to false when you are done with the event handling.

lbalazscs
  • 17,474
  • 7
  • 42
  • 50
  • What if I'm having an app like a music player that updates slider position practically all the time? I need to allow user to seek in file by dragging slider, but also update the slider as playback goes on. This means that setValue will be called programmatically all the time (say every 1/10-th of a second). So users slider drag event will always get ignored. – mvmn Oct 07 '21 at 08:56
  • @mvmn If you call setValue on the event dispatch tread, as you should, then it will be OK because the two calls will never overlap. – lbalazscs Nov 09 '21 at 13:08
  • Not sure how this is supposed to fix anything. I'm already setting value in EDT, but this triggers handler on change. – mvmn Nov 16 '21 at 10:38
1

Internally, ´setValue´ is invoked. You can try catching the event when the user moves the knob of the slider by implementing a ChangeListener to capture that event. Plus, remember that moving the knob triggers many change events, so if you're interested on the final value of the slider, make use of the getValueIsAdjusting when it evaluates to false.


If the problem is withing a ChangeListener, try extending the JSlider component and add a new method that receives the new value AND who issues it (with an int code or an enum, for example), deriving the setting of the value after you make your custom logic to the real setValue method.

In your case, you'll want to prevent the invokation of setValue if certain component invokes it, if I'm not mistaken.

Fritz
  • 9,987
  • 4
  • 30
  • 49
  • I've already caught the event, the problem is within the event because there are two objects interdependent and they change one anothers values so they get in an infinite loop, that's why I need to detect the mode of change. – andreihondrari Jan 14 '13 at 14:19