java left logo
java middle logo
java right logo
 

Home arrow Java SE Tips arrow javax.swing arrow How to use a slider with tick marks and labels
 
 
Main Menu
Home
Java Tutorials
Book Reviews
Java SE Tips
Java ME Tips
Java EE Tips
Other API Tips
Java Applications
Java Libraries
Java Games
Java Network
Java Forums
Java Blog




Most Visited Tips
Java SE Tips
Java ME Tips
Java EE Tips
Other API Tips
Java Applications
Java Libraries
Java Games
Book Reviews
Top Rated Tips
Java SE Tips
Java ME Tips
Java EE Tips
Other API Tips
Java Applications
Java Libraries
Java Games
Book Reviews


Statistics
Registered Users: 3947
Java SE Tips: 614
Java ME Tips: 202
Java EE Tips: 183
Other API Tips: 779
Java Applications: 298
Java Libraries: 209
Java Games: 16
Book Reviews:
 
 
 
How to use a slider with tick marks and labels E-mail
User Rating: / 4
PoorBest 

This Java Swing tip illustrates a method of implementing a slider with tick marks and labels. The JSlider used in tip used in a similar manner to Scrollbar: the zero-argument constructor creates a horizontal slider with a range from 0 to 100 and an initial value of 50. You can also supply the orientation (via JSlider.HORIZONTAL or JSlider.VERTICAL) and the range and initial value to the constructor. Developer may handle events by attaching a ChangeListener. Its stateChanged method normally calls getValue to look up the current JSlider value.

import java.awt.*;
import javax.swing.*;

public class JSliders extends JFrame {
    public static void main(String[] args) {
        new JSliders();
    }
    
    public JSliders() {
        super("Using JSlider");
        // Comment out next line for Java LAF
        WindowUtilities.setNativeLookAndFeel();
        addWindowListener(new ExitListener());
        Container content = getContentPane();
        content.setBackground(Color.white);
        
        JSlider slider1 = new JSlider();
        slider1.setBorder(BorderFactory.createTitledBorder(
                "JSlider without Tick Marks"));
        
        content.add(slider1, BorderLayout.NORTH);
        
        JSlider slider2 = new JSlider();
        slider2.setBorder(BorderFactory.createTitledBorder(
                "JSlider with Tick Marks"));
        
        slider2.setMajorTickSpacing(20);
        slider2.setMinorTickSpacing(5);
        slider2.setPaintTicks(true);
        content.add(slider2, BorderLayout.CENTER);
        
        JSlider slider3 = new JSlider();
        slider3.setBorder(BorderFactory.createTitledBorder(
                "JSlider with Tick Marks & Labels"));
        
        slider3.setMajorTickSpacing(20);
        slider3.setMinorTickSpacing(5);
        slider3.setPaintTicks(true);
        slider3.setPaintLabels(true);
        content.add(slider3, BorderLayout.SOUTH);
        
        pack();
        setVisible(true);
    }
}


 Related Tips

 
< Prev   Next >

Page 1 of 0 ( 0 comments )

You can share your information about this topic using the form below!

Please do not post your questions with this form! Thanks.


Name (required)


E-Mail (required)

Your email will not be displayed on the site - only to our administrator
Homepage(optional)



Comment Enable HTML code : Yes No



 
       
         
     
 
 
 
   
 
 
java bottom left
java bottom middle
java bottom right
RSS 0.91 FeedRSS 1.0 FeedRSS 2.0 FeedATOM FeedOPML Feed

Home - About Us - Privacy Policy
Copyright 2005 - 2008 www.java-tips.org
Java is a trademark of Sun Microsystems, Inc.