Set the size of a JSlider thumb
Asked Answered
L

2

10

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

Lobby answered 22/8, 2011 at 13:59 Comment(10)
How did you get it that skinny?Buckshot
It's a horizontal JSlider with min and max of 0 and 255 respectively, nothing special. When I reduce the range to 0 to 16, the thumb is the same size. When I change the minor or major tick spacing, the thumb is the same size. So I don't know why it's so skinny.Lobby
@Andrew Cecil, It would be cool if you included a screenshot. :DEngender
@Andrew How much is a few pixels? Which UI are you using?Wore
Enabling the paintTicks property (but with no ticks) gives the following: link. That will probably do.Lobby
See also this answer.Precision
@trashgod, You should make that an answer. :DEngender
@mre: Thank you; but, as Thomas notes, replacing the UI delegate is a last resort.Precision
"a better (although not preferred) display:" So what do you mean, wider still than the thumb in the 2nd screen-shot? Also I'm thinking maybe I should delete my 'answer', since your post now includes & explains the images better. Nice screenshots, BTW. :-)Pernik
@Andrew Thompson - I've added explanation to the question of the desired look. Part of this is because enabling ticks (even with none displayed) adds space below the slider which makes a tight layout tricky.Lobby
E
3

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.

Engender answered 22/8, 2011 at 14:6 Comment(3)
At what point does this need to be done? Before setting look and feel? Before creating JSlider components? So far I have been unable to get this to have any effect.Lobby
@Andrew, Good question. I don't know yet.Engender
Looks like these UIDefaults keys are only queried by Nimbus / Synth L&Fs (in Oracle Java at least)Cadell
W
2

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.

Wore answered 22/8, 2011 at 14:13 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.