The Java Developers Almanac 1.4


Order this book from Amazon.

   
Home > List of Packages > javax.swing  [141 examples] > JSlider  [7 examples]

e794. Creating a JSlider Component

    // Create a horizontal slider with min=0, max=100, value=50
    JSlider slider = new JSlider();
    
    // Create a horizontal slider with custom min and max; value is set to the middle
    int minimum = -255;
    int maximum = 256;
    slider = new JSlider(minimum, maximum);
    
    // Create a horizontal slider with custom min, max, and value
    int initValue = 0;
    slider = new JSlider(minimum, maximum, initValue);
    
    // Create a vertical slider with min=0, max=100, value=50
    slider = new JSlider(JSlider.VERTICAL);
    
    // Create a vertical slider with custom min, max, and value
    slider = new JSlider(JSlider.VERTICAL, minimum, maximum, initValue);
In addition to allowing the user to drag the slider, some look and feels allow the user page the slider by a fixed amount. This amount is called the extent.
    // Set an extent
    int extent = 10;
    slider.setExtent(extent);
For most look and feels, the slider appears as a knob on a track. In this case, it is possible to hide the track:
    slider.setPaintTrack(false);

 Related Examples
e795. Getting and Setting the Values of a JSlider Component
e796. Setting the Orientation of a JSlider Component
e797. Showing Tick Marks on a JSlider Component
e798. Showing Tick-Mark Labels on a JSlider Component
e799. Constraining JSlider Component Values to Tick Marks
e800. Listening for Value Changes in a JSlider Component

See also: Actions    JButton    JCheckBox    JComboBox    JDesktop and JInternalFrame    JFrame, JWindow, JDialog    JLabel    JList    JProgressBar    JRadioButton    JScrollPane    JSpinner    JSplitPane    JTabbedPane    JToolBar    Keystrokes and Input Maps    Layout    Look and Feel    Menus    Progress Monitor    The Screen    Tool Tips    UI Default Values   


© 2002 Addison-Wesley.