Remove value displaying over thumb in JSlider

假装没事ソ 提交于 2020-01-13 03:07:09

问题


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 ?


回答1:


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 the protected boolean paintValue being set in private void updateStyle(JSlider c), so if you were to make a custom extension this class, set the value of paintValue to false, and then set your slider's UI to the new UI with setUI(), 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):

import java.awt.BorderLayout;
import java.lang.reflect.Field;

import javax.swing.JFrame;
import javax.swing.JSlider;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;

public class SliderDemo {

    public static void main(String[] args) throws Exception {
        UIManager.setLookAndFeel("javax.swing.plaf.synth.SynthLookAndFeel");

        Class<?> sliderUIClass = Class.forName("javax.swing.plaf.synth.SynthSliderUI");
        final Field paintValue = sliderUIClass.getDeclaredField("paintValue");
        paintValue.setAccessible(true);

        SwingUtilities.invokeAndWait(new Runnable() {
            @Override
            public void run() {
                JFrame f = new JFrame();
                f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                f.setLayout(new BorderLayout());

                JSlider slider = new JSlider(3, 6, 4);
                try {
                    paintValue.set(slider.getUI(), false);
                } catch (Exception e) {
                    throw new RuntimeException(e);
                }
                f.add(slider, BorderLayout.CENTER);

                f.pack();
                f.setVisible(true);
            }
        });
    }

}



回答2:


Calling UIManager.put("Slider.paintValue", false) before the call to initComponents() worked for me.



来源:https://stackoverflow.com/questions/4460840/remove-value-displaying-over-thumb-in-jslider

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!