Easy to Learn Java: Programming Articles, Examples and Tips

Start with Java in a few days with Java Lessons or Lectures

Home

Code Examples

Java Tools

More Java Tools!

Java Forum

All Java Tips

Books

Submit News
Search the site here...
Search...
 
Search the JavaFAQ.nu
1000 Java Tips ebook

1000 Java Tips - Click here for the high resolution copy!1000 Java Tips - Click here for the high resolution copy!

Java Screensaver, take it here

Free "1000 Java Tips" eBook is here! It is huge collection of big and small Java programming articles and tips. Please take your copy here.

Take your copy of free "Java Technology Screensaver"!.

Java: Sliders

JavaFAQ Home » Swing, AWT Go to all tips in Swing, AWT


Bookmark and Share

Introduction

A slider (JSlider class) lets the user easily select from a range of integer values. Use sliders for integer input whenever you can. They are easier for the user than text fields, and there is no possibility of illegal input values, so your programming is simpler.

Constructors

The usual constructor is
   JSlider s = new JSlider(orientation, min, max, initial);
  • orientation: JSlider.HORIZONTAL or JSlider.VERTICAL
  • min: The minimum value.
  • max: The maximum value.
  • initial: The initial value.
Example:
JSlider sl = new JSlider(JSlider.HORIZONTAL, 0, 20, 10);

Tick Marks

You can add both major and minor tick marks. You must call setPaintTicks(true) to make the tick marks appear. Here is an example that sets major tick marks every 10 values and minor tick marks every 1 value:
   sl.setMajorTickSpacing(10); // sets numbers for biggest tick marks
   sl.setMinorTickSpacing(1);  // smaller tick marks
   sl.setPaintTicks(true);     // display the ticks

Labels

To display the values next to the major tick marks:
   sl.setPaintLabels(true);

Listeners and getting the slider value

See Change Listener to see how to do something when the slider is changed.

Advanced Appearance

In addition to tick marks, you can specify text or icon labels at any values that you specify using setLabelTable(...). You can also specify which end of the slider should have the high or low values using setInverted(true/false).
 Printer Friendly Page  Printer Friendly Page
 Send to a Friend  Send to a Friend

.. Bookmark and Share

Search here again if you need more info!
Custom Search



Home Code Examples Java Forum All Java Tips Books Submit News, Code... Search... Offshore Software Tech Doodling

RSS feed Java FAQ RSS feed Java FAQ News     

    RSS feed Java Forums RSS feed Java Forums

All logos and trademarks in this site are property of their respective owner. The comments are property of their posters, all the rest 1999-2006 by Java FAQs Daily Tips.

Interactive software released under GNU GPL, Code Credits, Privacy Policy