Get current/minimum/maximum value Values of a JSlider in Java

Description

The following code shows how to get current/minimum/maximum value Values of a JSlider.

Example


/*from  ww  w  . java 2 s.c o m*/
import javax.swing.JFrame;
import javax.swing.JSlider;

public class Main {
  public static void main(String[] args) {
    JFrame f = new JFrame();
    int initValue = 20;
    int minimum = 10;
    int maximum = 100;

    JSlider slider = new JSlider(JSlider.VERTICAL, minimum, maximum, initValue);
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    

    // Get the current value
    int value = slider.getValue();
    System.out.println(value);
    // Get the minimum value
    int min = slider.getMinimum();
    System.out.println(min);
    // Get the maximum value
    int max = slider.getMaximum();
    System.out.println(max);
    f.add(slider);
    f.pack();
    f.setLocationRelativeTo(null);
    f.setVisible(true);
  }
}

The code above generates the following result.

Get current/minimum/maximum value Values of a JSlider in Java




















Home »
  Java Tutorial »
    Swing »




Action
Border
Color Chooser
Drag and Drop
Event
Font Chooser
JButton
JCheckBox
JComboBox
JDialog
JEditorPane
JFileChooser
JFormattedText
JFrame
JLabel
JList
JOptionPane
JPasswordField
JProgressBar
JRadioButton
JScrollBar
JScrollPane
JSeparator
JSlider
JSpinner
JSplitPane
JTabbedPane
JTable
JTextArea
JTextField
JTextPane
JToggleButton
JToolTip
JTree
Layout
Menu
Timer