Example usage for javax.swing JScrollBar getBlockIncrement

List of usage examples for javax.swing JScrollBar getBlockIncrement

Introduction

In this page you can find the example usage for javax.swing JScrollBar getBlockIncrement.

Prototype

public int getBlockIncrement(int direction) 

Source Link

Document

Returns the amount to change the scrollbar's value by, given a block (usually "page") up/down request.

Usage

From source file:Main.java

/**
 * Scrolls the given component by its unit or block increment in the given direction (actually a scale factor, so use +1 or -1).
 * Useful for implementing behavior like in Apple's Mail where page up/page down in the list cause scrolling in the text.
 *//*from   www  . j a  v a 2  s  .c  o m*/
public static void scroll(JComponent c, boolean byBlock, int direction) {
    JScrollPane scrollPane = (JScrollPane) SwingUtilities.getAncestorOfClass(JScrollPane.class, c);
    JScrollBar scrollBar = scrollPane.getVerticalScrollBar();
    int increment = byBlock ? scrollBar.getBlockIncrement(direction) : scrollBar.getUnitIncrement(direction);
    int newValue = scrollBar.getValue() + direction * increment;
    newValue = Math.min(newValue, scrollBar.getMaximum());
    newValue = Math.max(newValue, scrollBar.getMinimum());
    scrollBar.setValue(newValue);
}

From source file:Main.java

public static void scrollByBlock(JScrollBar scrollbar, int direction) {
    // This method is called from BasicScrollPaneUI to implement wheel
    // scrolling, and also from scrollByBlock().
    int oldValue = scrollbar.getValue();
    int blockIncrement = scrollbar.getBlockIncrement(direction);
    int delta = blockIncrement * ((direction > 0) ? +1 : -1);
    int newValue = oldValue + delta;

    // Check for overflow.
    if (delta > 0 && newValue < oldValue) {
        newValue = scrollbar.getMaximum();
    } else if (delta < 0 && newValue > oldValue) {
        newValue = scrollbar.getMinimum();
    }//from www .j  ava 2 s . co  m

    scrollbar.setValue(newValue);
}