Sams Teach Yourself Java 2 in 24 Hours
- Table of Contents
- Copyright
- About the Author
- About the Technical Editor
- Acknowledgments
- We Want to Hear from You!
- Reader Services
- Introduction
- Hour 1. Becoming a Programmer
- Hour 2. Writing Your First Program
- Hour 3. Vacationing in Java
- Hour 4. Understanding How Java Programs Work
- Part II: Learning the Basics of Programming
- Hour 5. Storing and Changing Information in a Program
- Hour 6. Using Strings to Communicate
- Hour 7. Using Conditional Tests to Make Decisions
- Hour 8. Repeating an Action with Loops
- Part III: Working with Information in New Ways
- Hour 9. Storing Information with Arrays
- Hour 10. Creating Your First Object
- Hour 11. Describing What Your Object Is Like
- Hour 12. Making the Most of Existing Objects
- Part IV: Programming a Graphical User Interface
- Hour 13. Building a Simple User Interface
- Hour 14. Laying Out a User Interface
- Hour 15. Responding to User Input
- Hour 16. Building a Complex User Interface
- Part V: Creating Multimedia Programs
- Hour 17. Creating Interactive Web Programs
- Hour 18. Handling Errors in a Program
- Hour 19. Creating a Threaded Program
- Hour 20. Reading and Writing Files
- Part VI: Creating Multimedia Programs
- Hour 21. Using Fonts and Color
- Hour 22. Playing Sound Files
- Hour 23. Working with Graphics
- Hour 24. Creating Animation
- Part VII: Appendixes
- Appendix A. Tackling New Features of Java 2 Version 1.4
- Appendix B. Using the Java 2 Software Development Kit
- Appendix C. Programming with the Java 2 Software Development Kit
- Appendix D. Using Sun ONE Studio
- Appendix E. Where to Go from Here: Java Resources
- Appendix F. This Book's Web Site
Sliders
One of the easiest ways for a user to enter numeric input is by using a slider, a component that can be dragged from side to side or up and down.
Sliders are represented in Swing by the JSlider class. Figure 16.2 shows what a slider component looks like.
Figure 16.2 Displaying a slider component.
Sliders enable a number to be chosen from between a minimum and maximum range of values. These values can be displayed on a label that includes the minimum value, maximum value, and intermediate values (as shown later in Figure 16.3).
Figure 16.3 Choosing a color using slider components.
You can create a horizontal slider with one of the following constructors:
- JSlider( int , int )— Create a slider with the specified minimum value and maximum value.
- JSlider( int , int , int )— Create a slider with the specified minimum value, maximum value, and starting value.
To create a vertical slider, you must use a constructor method with an additional argument—the orientation of the slider. This argument should be the class variables JSlider.VERTICAL or JSlider.HORIZONTAL.
The following statement creates a vertical slider that can be used to pick a number from 1 to 1,000:
JSlider guess = new JSlider(JSlider.VERTICAL, 1, 1000, 500);
This slider starts with the caret—the part of the component used to select a number—at the 500 position.
To display a label for a slider, you must set up the information that the label will contain. Call the slider's setMajorTickSpacing( int ) and setMinorTickSpacing( int ) methods to determine how often a tick mark will be displayed on the label. Major ticks are displayed as a thicker line than minor ticks.
After you have set up how often tick marks will appear, call the slider's setPaintTicks( boolean ) method with true as the argument.
You can also display the numeric value of each major tick by calling the slider's
setPaintLabels( boolean ) method with true.
The following statements can be used to create the slider shown in Figure 16.2:
JSlider percentage = new JSlider(0, 100, 25); percentage.setMajorTickSpacing(10); percentage.setMinorTickSpacing(5); percentage.setPaintTicks(true); percentage.setPaintLabels(true);
Change Listeners | Next Section

Account Sign In
View your cart