JSlider : Java Glossary

*0-9ABCDEFGHIJKLMNOPQRSTUVWXYZ (all)

JSlider
A Swing JComponent that lets the user drag a thumbtab horizontally or vertically to continuously change a numeric value. For example, a JSlider can be used to control magnification continuously. You can redraw as the slider moves. I used it in one app to control the speed of an animation.

It does not have a built-in indicator of the current value. It will label the slider with tick marks and calibrations. By default, the labels of the calibrations are in integer ticks. To get something else, you need a lookup Hashtable<Integer,JLabel> (not String as you would expect) where the JLabels display text or an icon for the important ticks. You have no control over the Font or Color of the calibrations. They take on the the colour of the JFrame they are embedded in. If you try too big a range on too short a slider, you get gibberish since the calibrations overlap. JSliders work for both negative and positive values.

The catch is JSlider works only with ints. If you want to slide over a range of decimal values or display calibrations with decimals, or have ticks at every second stop, you will have to fake it yourself with integers and roll your own calibrations.

Here is what one looks like with custom calibrations. Note this one lets you position the slider half-way between ticks.

JSlider example

Here is the basic code you will need.

Learning More

Oracle’s Javadoc on JSlider class : available:

This page is posted
on the web at:

http://mindprod.com/jgloss/jslider.html

Optional Replicator mirror
of mindprod.com
on local hard disk J:

J:\mindprod\jgloss\jslider.html
Canadian Mind Products
Please the feedback from other visitors, or your own feedback about the site.
Contact Roedy. Please feel free to link to this page without explicit permission.

IP:[65.110.21.43]
Your face IP:[18.97.14.84]
You are visitor number