Example usage for javax.swing JSlider getValue

List of usage examples for javax.swing JSlider getValue

Introduction

In this page you can find the example usage for javax.swing JSlider getValue.

Prototype

public int getValue() 

Source Link

Document

Returns the slider's current value from the BoundedRangeModel .

Usage

From source file:Main.java

public static void main(String[] argv) throws Exception {
    JSlider slider = new JSlider();

    slider.setValue(27);//from ww  w.  ja v  a  2  s .  c o  m
    int value = slider.getValue();
}

From source file:Main.java

public static void main(String[] argv) throws Exception {
    JSlider slider = new JSlider();

    // Get the current value
    int value = slider.getValue();

    // Get the minimum value
    int min = slider.getMinimum();

    // Get the maximum value
    int max = slider.getMaximum();
}

From source file:Main.java

public static void main(String args[]) {
    JFrame frame = new JFrame();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    JSlider oneJSlider = new JSlider();

    oneJSlider.setValue(27);//from   ww  w . java  2  s. co  m

    int value = oneJSlider.getValue();
    System.out.println(value);

    oneJSlider.setPaintTicks(true);

    frame.add(oneJSlider, BorderLayout.NORTH);
    frame.setSize(300, 200);
    frame.setVisible(true);
}

From source file:RegisterChangeListenerToJSlider.java

public static void main(String[] a) {
    JFrame frame = new JFrame();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    JSlider framesPerSecond = new JSlider(JSlider.HORIZONTAL, 10, 100, 20);
    framesPerSecond.addChangeListener(new ChangeListener() {
        public void stateChanged(ChangeEvent e) {
            JSlider source = (JSlider) e.getSource();
            if (!source.getValueIsAdjusting()) {
                int fps = (int) source.getValue();
                System.out.println(fps);
            }/*from w ww . ja  v a2  s  . c o m*/
        }
    });

    frame.add(framesPerSecond, "North");

    frame.setSize(300, 200);
    frame.setVisible(true);
}

From source file:Main.java

public static void main(String args[]) {
    JFrame frame = new JFrame();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    JSlider oneJSlider = new JSlider();

    int newValue = 2;
    int newExtent = 10;
    int newMin = 0;
    int newMax = 10;
    oneJSlider.getModel().setRangeProperties(newValue, newExtent, newMin, newMax, true);
    int value = oneJSlider.getValue();
    System.out.println(value);//from  w  w w . ja va 2s  . c o m

    oneJSlider.setPaintTicks(true);

    frame.add(oneJSlider, BorderLayout.NORTH);
    frame.setSize(300, 200);
    frame.setVisible(true);
}

From source file:Main.java

public static void main(String[] argv) throws Exception {
    JSlider slider = new JSlider();
    slider.addChangeListener(new ChangeListener() {
        public void stateChanged(ChangeEvent evt) {
            JSlider slider = (JSlider) evt.getSource();
            if (!slider.getValueIsAdjusting()) {
                int value = slider.getValue();
                System.out.println(value);
            }/*from  w w  w .  j  a  va  2s  .c om*/
        }
    });
}

From source file:DynamicIconExample.java

public static void main(String[] args) {
    final JSlider width = new JSlider(JSlider.HORIZONTAL, 1, 150, 75);
    final JSlider height = new JSlider(JSlider.VERTICAL, 1, 150, 75);

    class DynamicIcon implements Icon {
        public int getIconWidth() {
            return width.getValue();
        }/*from w  w w  .ja  v a2  s  .  c  o  m*/

        public int getIconHeight() {
            return height.getValue();
        }

        public void paintIcon(Component c, Graphics g, int x, int y) {
            g.fill3DRect(x, y, getIconWidth(), getIconHeight(), true);
        }
    }

    Icon icon = new DynamicIcon();
    final JLabel dynamicLabel = new JLabel(icon);

    class Updater implements ChangeListener {
        public void stateChanged(ChangeEvent ev) {
            dynamicLabel.repaint();
        }
    }
    Updater updater = new Updater();

    width.addChangeListener(updater);
    height.addChangeListener(updater);

    JFrame f = new JFrame();
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    Container c = f.getContentPane();
    c.setLayout(new BorderLayout());
    c.add(width, BorderLayout.NORTH);
    c.add(height, BorderLayout.WEST);
    c.add(dynamicLabel, BorderLayout.CENTER);
    f.setSize(210, 210);
    f.setVisible(true);
}

From source file:Main.java

public static void main(String[] args) {
    JFrame f = new JFrame();
    final JSlider slider = new JSlider(0, 150, 0);
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    slider.setPreferredSize(new Dimension(150, 30));
    slider.addChangeListener(new ChangeListener() {
        public void stateChanged(ChangeEvent event) {
            int value = slider.getValue();
            if (value == 0) {
                System.out.println("0");
            } else if (value > 0 && value <= 30) {
                System.out.println("value > 0 && value <= 30");
            } else if (value > 30 && value < 80) {
                System.out.println("value > 30 && value < 80");
            } else {
                System.out.println("max");
            }/*  w w w  . ja v  a 2 s .c  o m*/
        }
    });
    f.add(slider);
    f.pack();
    f.setLocationRelativeTo(null);
    f.setVisible(true);
}

From source file:Main.java

public static void main(String[] args) {
    JFrame frame = new JFrame();

    JPanel main = new JPanel(new GridLayout(2, 1));
    JPanel scrollBarPanel = new JPanel();
    final JScrollBar scrollBar = new JScrollBar(JScrollBar.HORIZONTAL, 0, 48, 0, 255);
    int height = scrollBar.getPreferredSize().height;
    scrollBar.setPreferredSize(new Dimension(175, height));
    scrollBarPanel.add(scrollBar);//from   w ww . j a  va2s  .  c o m
    main.add(scrollBarPanel);

    JPanel sliderPanel = new JPanel();
    final JSlider slider = new JSlider(JSlider.HORIZONTAL, 0, 255, 128);
    slider.setMajorTickSpacing(48);
    slider.setMinorTickSpacing(16);
    slider.setPaintTicks(true);
    sliderPanel.add(slider);
    main.add(sliderPanel);

    frame.add(main, BorderLayout.CENTER);

    scrollBar.addAdjustmentListener(new AdjustmentListener() {
        public void adjustmentValueChanged(AdjustmentEvent e) {
            System.out.println("JScrollBar's current value = " + scrollBar.getValue());
        }
    });

    slider.addChangeListener(new ChangeListener() {
        public void stateChanged(ChangeEvent e) {
            System.out.println("JSlider's current value = " + slider.getValue());
        }
    });

    frame.pack();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setVisible(true);
}

From source file:JSliderOnJOptionPane.java

static JSlider getSlider(final JOptionPane optionPane) {
    JSlider slider = new JSlider();
    slider.setMajorTickSpacing(10);//from   w  w w .  j a v  a2  s .c  o  m
    slider.setPaintTicks(true);
    slider.setPaintLabels(true);
    ChangeListener changeListener = new ChangeListener() {
        public void stateChanged(ChangeEvent changeEvent) {
            JSlider theSlider = (JSlider) changeEvent.getSource();
            if (!theSlider.getValueIsAdjusting()) {
                optionPane.setInputValue(new Integer(theSlider.getValue()));
            }
        }
    };
    slider.addChangeListener(changeListener);
    return slider;
}