JSlide in Java Swing

Home  »  Java  »  Advance Java  »  Java Swing II  »  JSlide


Sliders that are implemented in Swing through the JSlider class enables a number to be set by sliding a control within the range of a minimum and maximum values. In many cases, a slider can be used for numeric input instead of a text field, and it has the advantage of restricting input to a range of acceptable values.

A Slider is often seen in media players to adjust the audio volume, channel frequencies, contrast or brightness of picture, and so on.

Sliders are horizontaly by default. The orientation can be explicity set by using two class variables of the SwingConstants class: HORIZONTAL or VERICAL.

The following are the JSlider constructor methods:

  • JSlider(int, int)
  • Slider with the specified minimum value and maximum value.
  • JSlider(int, int, int)
  • Slider with the specified minimum value, maximum value, maximum value and starting value.
  • JSlider(int, int, int, int)
  • Slider with the specified orientation, minimum value, maximum value and staring value.

    Slider component have an optional label that can be used to indicate the minimum value, maximum value and two different sets of tick marks rangining between the values.

    The elements of this label are established by calling several methods of JSlider:

  • setMajorTickSpacing(int)
  • Separate major tick marks by the specified distance. The distance is not in pixels, but in value between the minimum and maximum values represented by the slider.

  • setMinorTickSpacing(int)
  • Separate minor tick mark by the specified distance. Minor ticks are half as tall as major ticks.

  • setPaintTicks(boolean)
  • Determine whether the tick marks should be displayed (a true argument) or not (a false argument).
  • setPaintLabels(boolean)
  • Determine whether the numeric label of the slider should be displayed (true) or not (false.).

    These methods should be called on the slider before it is added to a container.

    Example:

    The following displays a slider that can control the speed at which a picture is animated.



    C:\>jdk1.4\bin>javac JSliderDemo.java
    C:\>jdk1.4\bin>java JSliderDemo


    "Output of JSliderDemo.class"


    Download Complete Program

      -  



    Java Swing-II


    JOptionPane Class Input Dialog Boxes JSliders
    JProgressBars JTables JTabbedPane
    JMenu JPopupMenu JToolBar
    JFileChooser JScrollBar JScrollPane
    JRootPane JTextPane JTree
    JSplitPane JDesktopPane JEditorPane




    1 comment: