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

  • 1
    How did you get it _that_ skinny? – Catalina Island Aug 22 '11 at 14:03
  • 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. –  Aug 22 '11 at 14:07
  • 2
    @Andrew Cecil, It would be cool if you included a screenshot. :D – mre Aug 22 '11 at 14:09
  • 1
    @Andrew How much is `a few pixels`? Which UI are you using? – Thomas Aug 22 '11 at 14:14
  • 2
    Enabling the `paintTicks` property (but with no ticks) gives the following: [link](http://imageshack.us/photo/my-images/819/jslider2.png). That will probably do. –  Aug 22 '11 at 14:46
  • 4
    See also this [answer](http://stackoverflow.com/questions/6992633/painting-the-slider-icon-of-jslider/6996263#6996263). – trashgod Aug 22 '11 at 15:32
  • 1
    @trashgod, You should make that an answer. :D – mre Aug 22 '11 at 15:49
  • @mre: Thank you; but, as Thomas notes, replacing the UI delegate is a last resort. – trashgod Aug 22 '11 at 16:13
  • 1
    *"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. :-) – Andrew Thompson Aug 22 '11 at 16:21
  • 1
    @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. –  Aug 22 '11 at 16:36

2 Answers2

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.

mre
  • 43,520
  • 33
  • 120
  • 170
  • 4
    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. –  Aug 22 '11 at 14:35
  • 1
    @Andrew, Good question. I don't know yet. – mre Aug 22 '11 at 14:59
  • 1
    Looks like these UIDefaults keys are only queried by Nimbus / Synth L&Fs (in Oracle Java at least) – Luke Usherwood Oct 28 '16 at 11:48
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.

Thomas
  • 87,414
  • 12
  • 119
  • 157