Scrolls the given JComponent to its top or bottom. - Java Swing

Java examples for Swing:JComponent

Description

Scrolls the given JComponent to its top or bottom.

Demo Code


//package com.java2s;

import javax.swing.*;

public class Main {
    /**//from   w ww  .j a va2s.co  m
     * Scrolls the given component to its top or bottom.
     * Useful for implementing behavior like in Apple's Mail where home/end in the list cause scrolling in the text.
     */
    public static void scrollToExtremity(JComponent c, boolean top) {
        JScrollBar scrollBar = ((JScrollPane) SwingUtilities
                .getAncestorOfClass(JScrollPane.class, c))
                .getVerticalScrollBar();
        scrollBar.setValue(top ? scrollBar.getMinimum() : scrollBar
                .getMaximum());
    }
}

Related Tutorials