Whenever I click a JSlider it gets positioned one majorTick in the direction of the click instead of jumping to the spot I actually click. (If slider is at point 47 and I click 5 it'll jump to 37 instead of 5). Is there any way to change this while using JSliders, or do I have to use another datastructure?
相关问题
- Delete Messages from a Topic in Apache Kafka
- Jackson Deserialization not calling deserialize on
- How to maintain order of key-value in DataFrame sa
- StackExchange API - Deserialize Date in JSON Respo
- Difference between Types.INTEGER and Types.NULL in
As bizarre as this might seem, it's actually the Look and Feel which controls this behaviour. Take a look at
BasicSliderUI
, the method that you need to override isscrollDueToClickInTrack(int)
.In order to set the value of the
JSlider
to the nearest value to where the user clicked on the track, you'd need to do some fancy pants translation between the mouse coordinates fromgetMousePosition()
to a valid track value, taking into account the position of theComponent
, it's orientation, size and distance between ticks etc. Luckily,BasicSliderUI
gives us two handy functions to do this:valueForXPosition(int xPos)
andvalueForYPosition(int yPos)
:This question is kind of old, but I just ran across this problem myself. This is my solution:
This behavior is derived from OS. Are you sure you want to redefine it and confuse users? I don't think so. ;)