How to check if value was set manually and not with setValue (JSlider, JSpinner)?
Asked Answered
M

2

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

Minesweeper answered 14/1, 2013 at 14:12 Comment(1)
Graphical interface internally calls setValue so no wayDefiniens
L
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.

Led answered 14/1, 2013 at 14:23 Comment(3)
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.Cumuliform
@Cumuliform If you call setValue on the event dispatch tread, as you should, then it will be OK because the two calls will never overlap.Led
Not sure how this is supposed to fix anything. I'm already setting value in EDT, but this triggers handler on change.Cumuliform
F
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.

Feretory answered 14/1, 2013 at 14:17 Comment(1)
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.Minesweeper

© 2022 - 2024 — McMap. All rights reserved.