I have a little problem with my JSlider, that I haven't been able to solve. To explain the situation a little, I have to do a JSlider going from 0 to 20, through 0.1 steps. I tricked my way out of problems by creating a JSlider from 0 to 200, and redefined the labels under the slider to display the corrent percentages instead of the integer values.
But I have one last problem : I'm using a custom L&F (that I can't change, obviously, as it is from the client), that displays the value over the slider thumb. However, this value is displayed...in its standard way, as an integer. From what I've been able to grasp, this display is related to the Slider.paintValue
property, as I can see in javax.swing.plaf.synth.SynthSliderUI
source code. However, I've been dramatically unable to remove it.
What I've tried for now :
UIManager.getLookAndFeelDefaults().put("Slider.paintValue", false);
UIManager.put("Slider.paintValue", false);
None of those two changed anything.
Is there a Swing guru around here who could get me out of this ?
Calling
UIManager.put("Slider.paintValue", false)
before the call toinitComponents()
worked for me.I just created a demo with a
JSlider
and using the basic Synth LAF, and I can confirm that setting those properties appears to have no effect at all, which is odd, and very annoying too.I have looked at
SynthSliderUI
and I can see theprotected boolean paintValue
being set inprivate void updateStyle(JSlider c)
, so if you were to make a custom extension this class, set the value ofpaintValue
to false, and then set your slider's UI to the new UI withsetUI()
, you might get closer. Either that, or completely override the paint methods, copying code out of the original but removing the part that paints the thumb value.Now,
SynthSliderUI
itself is package-private, but that isn't really the issue anyway since you're using a client-provided LAF. If their custom slider UI class is public and non-final, you can make a hacked version as I described above, and apply it to your slider.If it's final, then you might be able to use reflection to change that
paintValue
field as a last resort (maybe just before painting your slider). The following hack works for me (take out the reflection call that sets the field to false to see the thumb value being painted):