Example usage for javax.swing JSlider addChangeListener

List of usage examples for javax.swing JSlider addChangeListener

Introduction

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

Prototype

public void addChangeListener(ChangeListener l) 

Source Link

Document

Adds a ChangeListener to the slider.

Usage

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  v a  2s  .c o m
        }
    });
}

From source file:BoundedChangeListener.java

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

    JSlider jSliderOne = new JSlider();
    jSliderOne.addChangeListener(new BoundedChangeListener());
    frame.add(jSliderOne, BorderLayout.NORTH);
    frame.setSize(300, 200);//from www.j av  a 2  s . c o m
    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  www.ja v a2 s. c  o m*/
        }
    });

    frame.add(framesPerSecond, "North");

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

From source file:ScrollSlider.java

public static void main(String args[]) {
    JFrame f = new JFrame("Change Slider");
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    JSlider aJSlider = new JSlider(JSlider.HORIZONTAL, 0, 1000, 500);
    ChangeListener aChangeListener = new BoundedChangeListener();
    aJSlider.addChangeListener(aChangeListener);
    Container c = f.getContentPane();
    c.add(aJSlider, BorderLayout.SOUTH);
    f.setSize(300, 200);/*from w ww.j a  v a  2  s.c o  m*/
    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");
            }//from   w  ww. j  a 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);/*w ww  .  j a  v  a  2  s  . 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: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();
        }/* www  . j  av a2s.co  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:JSliderOnJOptionPane.java

static JSlider getSlider(final JOptionPane optionPane) {
    JSlider slider = new JSlider();
    slider.setMajorTickSpacing(10);/*from   w  w  w. j a  va 2 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;
}

From source file:MessagePopup.java

public static JSlider getSlider(final JOptionPane optionPane) {
    JSlider slider = new JSlider();
    slider.setMajorTickSpacing(10);//from   w  ww.ja v a 2s . co 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;
}

From source file:ColorComposite.java

public ColorComposite() {
    super();/*from w  ww  . j a va  2s  . com*/
    Container container = getContentPane();

    canvas = new MyCanvas();
    container.add(canvas);

    JPanel panel = new JPanel();

    JLabel label = new JLabel("Color-Composite: ");

    JSlider slider = new JSlider(JSlider.HORIZONTAL, 0, 100, 65);
    slider.addChangeListener(new ChangeListener() {
        public void stateChanged(ChangeEvent e) {
            JSlider tempSlider = (JSlider) e.getSource();
            alphaValue = (float) (tempSlider.getValue() / 100.0);
            textField.setText(Float.toString(alphaValue));
            canvas.repaint();
        }
    });

    textField = new JTextField("0.65", 4);

    panel.add(label);
    panel.add(slider);
    panel.add(textField);

    container.add(BorderLayout.SOUTH, panel);

    addWindowListener(new WindowAdapter() {
        public void windowClosing(WindowEvent e) {
            System.exit(0);
        }
    });
    setSize(450, 450);
    setVisible(true);
}