How to hide the knob of jSlider?

后端 未结 3 1192
后悔当初
后悔当初 2021-01-22 17:01

I need to customize the knob of JSlider. I need to put my own knob\'s image over default knob of Jslider. The problem is that currently two knobs are coming in response. One my

3条回答
  •  广开言路
    2021-01-22 17:36

    To hide the knob, override the UIManager's Slider.horizontalThumbIcon property with an blank icon, like this:

    public static void main(String[] args) throws Exception {
    
        UIManager.getLookAndFeelDefaults().put("Slider.horizontalThumbIcon",new Icon(){
            @Override
            public int getIconHeight() {
                return 0;
            }
            @Override
            public int getIconWidth() {
                return 0;
            }
            @Override
            public void paintIcon(Component c, Graphics g, int x, int y) {
                //do nothing
            }
        });
    
        JFrame f = new JFrame();
        JSlider slider = new JSlider(JSlider.HORIZONTAL, 0, 30, 15);
        slider.setMajorTickSpacing(10);
        slider.setMinorTickSpacing(1);
        slider.setPaintTicks(true);
        slider.setPaintLabels(true);
    
        f.add(slider);
        f.setSize(200,200);
        f.setVisible(true);
    
    }
    

提交回复
热议问题