Example usage for javax.swing BoundedRangeModel getValue

List of usage examples for javax.swing BoundedRangeModel getValue

Introduction

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

Prototype

int getValue();

Source Link

Document

Returns the model's current value.

Usage

From source file:Main.java

public static void main(String args[]) throws Exception {
    ChangeListener changeListener = new BoundedChangeListener();
    JScrollBar anotherJScrollBar = new JScrollBar(JScrollBar.HORIZONTAL);
    BoundedRangeModel model = anotherJScrollBar.getModel();
    model.addChangeListener(changeListener);
    System.out.println(model.getValue());

    JFrame frame = new JFrame("ScrollBars R Us");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.add(anotherJScrollBar, BorderLayout.NORTH);

    frame.setSize(300, 200);/*from   w  w  w.j  ava2s.  c o m*/
    frame.setVisible(true);
}

From source file:Main.java

/**
 * Creates a JSlider built/*from  w w w  .  j a v a 2 s.  co  m*/
 * using a {@link DefaultBoundedRangeModel} and 
 * containing a {@link MouseWheelListener} 
 * and some usual default settings
 * 
 * @param model
 * @return the corresponding {@link JSlider}
 */
public static JSlider createJSlider(final BoundedRangeModel model) {
    final JSlider slider = new JSlider(model);
    slider.addMouseWheelListener(new MouseWheelListener() {
        @Override
        public void mouseWheelMoved(MouseWheelEvent e) {
            slider.setValue(-e.getWheelRotation() * model.getMaximum() / 100 + model.getValue());
        }
    });
    slider.setPaintTicks(true);
    slider.setPaintLabels(true);
    //      slider.setMajorTickSpacing(model.getMaximum()/2);
    //      slider.setMinorTickSpacing(model.getMaximum()/10);
    //      slider.setSnapToTicks(true);
    return slider;
}

From source file:Main.java

public Main() {
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    JSlider slider = new JSlider();
    BoundedRangeModel model = slider.getModel();
    model.addChangeListener(new ChangeListener() {
        public void stateChanged(ChangeEvent e) {
            BoundedRangeModel m = (BoundedRangeModel) e.getSource();
            System.out.println("Slider position changed to " + m.getValue());
        }//from   w w w  .ja  va2  s . c  o  m
    });
    getContentPane().add(slider);
    pack();
}

From source file:BoundedChangeListener.java

public void stateChanged(ChangeEvent changeEvent) {
    Object source = changeEvent.getSource();
    if (source instanceof BoundedRangeModel) {
        BoundedRangeModel aModel = (BoundedRangeModel) source;
        if (!aModel.getValueIsAdjusting()) {
            System.out.println("Changed: " + aModel.getValue());
        }//from  ww w .  j av  a2 s .  c  om
    } else {
        System.out.println("Something changed: " + source);
    }
}

From source file:BoundedChangeListener.java

public void stateChanged(ChangeEvent changeEvent) {
    Object source = changeEvent.getSource();
    if (source instanceof BoundedRangeModel) {
        BoundedRangeModel aModel = (BoundedRangeModel) source;
        if (!aModel.getValueIsAdjusting()) {
            System.out.println("Changed: " + aModel.getValue());
        }/*w  w w.jav  a 2s  .  c om*/
    } else if (source instanceof JSlider) {
        JSlider theJSlider = (JSlider) source;
        if (!theJSlider.getValueIsAdjusting()) {
            System.out.println("Slider changed: " + theJSlider.getValue());
        }
    } else {
        System.out.println("Something changed: " + source);
    }
}

From source file:Main.java

public void stateChanged(ChangeEvent changeEvent) {
    Object source = changeEvent.getSource();
    if (source instanceof BoundedRangeModel) {
        BoundedRangeModel aModel = (BoundedRangeModel) source;
        if (!aModel.getValueIsAdjusting()) {
            System.out.println("Changed: " + aModel.getValue());
        }/* w  w w  .j ava  2 s  .  c o  m*/
    } else if (source instanceof JSlider) {
        JSlider theJSlider = (JSlider) source;
        if (!theJSlider.getValueIsAdjusting()) {
            System.out.println("Slider changed: " + theJSlider.getValue());
        }
    } else if (source instanceof JProgressBar) {
        JProgressBar theJProgressBar = (JProgressBar) source;
        System.out.println("ProgressBar changed: " + theJProgressBar.getValue());
    } else {
        System.out.println("Something changed: " + source);
    }
}

From source file:de.unibayreuth.bayeos.goat.panels.timeseries.JPanelChart.java

public void stateChanged(ChangeEvent event) {
    try {/*from   w  w  w .j  a va2 s  . c  om*/
        Object src = event.getSource();
        BoundedRangeModel scrollBarModel = this.chartScrollBar.getModel();
        if (src == scrollBarModel) {
            int val = scrollBarModel.getValue();
            int ext = scrollBarModel.getExtent();

            Plot plot = this.chartPanel.getChart().getPlot();
            if (plot instanceof XYPlot) {
                XYPlot hvp = (XYPlot) plot;
                ValueAxis axis = hvp.getDomainAxis();

                // avoid problems
                this.chartPanel.getChart().removeChangeListener(this);

                axis.setRange(val / scrollFactor, (val + ext) / scrollFactor);

                // restore chart listener
                this.chartPanel.getChart().addChangeListener(this);
            }
        }
    } catch (Exception e) {
        MsgBox.error(e.getMessage());
    }
}

From source file:apidemo.PanScrollZoomDemo.java

/**
 * Handles a {@link ChangeEvent} (in this case, coming from the scrollbar).
 * //from   w ww. ja  v  a  2s  .  c o m
 * @param event  the event.
 */
public void stateChanged(final ChangeEvent event) {
    try {
        final Object src = event.getSource();
        final BoundedRangeModel scrollBarModel = this.scrollBar.getModel();
        if (src == scrollBarModel) {
            final int val = scrollBarModel.getValue();
            final int ext = scrollBarModel.getExtent();

            final Plot plot = this.chartPanel.getChart().getPlot();
            if (plot instanceof XYPlot) {
                final XYPlot hvp = (XYPlot) plot;
                final ValueAxis axis = hvp.getDomainAxis();

                // avoid problems
                this.chartPanel.getChart().removeChangeListener(this);

                axis.setRange(val / this.scrollFactor, (val + ext) / this.scrollFactor);

                // restore chart listener
                this.chartPanel.getChart().addChangeListener(this);
            }
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:net.java.sip.communicator.impl.gui.main.chat.ChatConversationPanel.java

/**
 * Overrides Component#setBounds(int, int, int, int) in order to determine
 * whether an automatic scroll of #chatTextPane to its bottom will be
 * necessary at a later time in order to keep its vertical scroll bar to its
 * bottom after the realization of the resize if it is at its bottom before
 * the resize.//  w  w w.ja  va2  s  .c  om
 */
@Override
public void setBounds(int x, int y, int width, int height) {
    synchronized (scrollToBottomRunnable) {
        JScrollBar verticalScrollBar = getVerticalScrollBar();

        if (verticalScrollBar != null) {
            BoundedRangeModel verticalScrollBarModel = verticalScrollBar.getModel();

            if ((verticalScrollBarModel.getValue()
                    + verticalScrollBarModel.getExtent() >= verticalScrollBarModel.getMaximum())
                    || !verticalScrollBar.isVisible())
                scrollToBottomIsPending = true;
        }
    }

    super.setBounds(x, y, width, height);
}