Set the size of a JSlider thumb

2019-01-25 08:52发布

How can the size of the thumb be configured for a JSlider?

With the defaults, and a range for the JSlider of 256, the thumb is only a few pixels wide, which makes it quite difficult to control with a mouse.

I am using the Windows 7 look and feel and the slider looks like this:

Screenshot of slider

Enabling paintTicks with a major and minor tick spacing of 0 gives a better (although not preferred) display:

Screenshot of slider with paintTicks enabled

The desired display is shown in the following image - taken from a native Windows 7 application:

Screenshot of native slider

2条回答
我只想做你的唯一
2楼-- · 2019-01-25 09:23

This depends on the SliderUI, which might have hard coded sizes. If not, using mre`s suggestion would be a way to go, if you want the same thumb size for all sliders.

Alternatively to setting the defaults for a UI that uses them, you could define a different UI for a special slider (e.g. myslider.setUI(new MyCustonSliderUI())), but be aware that that has its own drawbacks.

查看更多
Bombasti
3楼-- · 2019-01-25 09:35

You could try customizing the JSlider Look and Feel as follows:

UIDefaults defaults = UIManager.getDefaults();
defaults.put("Slider.thumbHeight", HEIGHT_AS_INTEGER); // change height
defaults.put("Slider.thumbWidth", WIDTH_AS_INTEGER); // change width

Reference:

It's important to note that these changes will apply to all JSlider instances, which may make this approach undesirable.

查看更多
登录 后发表回答