Java JScrollPane Scroll scrollVertically(JComponent c, Rectangle r)

Here you can find the source of scrollVertically(JComponent c, Rectangle r)

Description

scroll Vertically

License

Apache License

Declaration

public static void scrollVertically(JComponent c, Rectangle r) 

Method Source Code


//package com.java2s;
/* /*w ww .  j  a  v  a 2s  . co m*/
 * Copyright 2003,2004,2005 Colin Crist
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * 
 */

import java.awt.Component;
import java.awt.Container;

import java.awt.Rectangle;

import javax.swing.JComponent;

import javax.swing.JViewport;

public class Main {
    public static final int VIEWPORT = 0, // take the policy of the viewport
            UNCHANGED = 1, // don't scroll if it fills the visible area, otherwise
            // take the policy of the viewport
            FIRST = 2, // scroll the first part of the region into view
            CENTER = 3, // center the region
            LAST = 4;

    public static void scrollVertically(JComponent c, Rectangle r) {
        scrollVertically(c, r.y, r.y + r.height);
    }

    public static void scrollVertically(JComponent c, int from, int to) {
        Rectangle visible = c.getVisibleRect();

        if (visible.y <= from && visible.y + visible.height >= to)
            return;

        visible.y = from;
        visible.height = to - from;

        c.scrollRectToVisible(visible);
    }

    public static void scrollVertically(JComponent c, Rectangle r, int bias) {
        scrollVertically(c, r.y, r.y + r.height, bias);
    }

    public static void scrollVertically(JComponent c, int from, int to, int bias) {
        Rectangle visible = c.getVisibleRect(), dest = new Rectangle(visible);

        dest.y = from;
        dest.height = to - from;

        if (dest.height > visible.height) {
            if (bias == VIEWPORT) {
                // leave as is
            } else if (bias == UNCHANGED) {
                if (dest.y <= visible.y && dest.y + dest.height >= visible.y + visible.height) {
                    dest.y = visible.y;
                    dest.height = visible.height;
                }
            } else {
                if (bias == CENTER)
                    dest.y += (dest.height - visible.height) / 2;
                else if (bias == LAST)
                    dest.y += dest.height - visible.height;

                dest.height = visible.height;
            }
        }

        if (!visible.contains(dest))
            c.scrollRectToVisible(dest);
    }

    public static void scrollRectToVisible(Component component, Rectangle aRect) {
        Container parent;
        int dx = component.getX(), dy = component.getY();

        for (parent = component.getParent(); parent != null
                && (!(parent instanceof JViewport) || (((JViewport) parent)
                        .getClientProperty("HierarchicalTable.mainViewport") == null)); parent = parent
                                .getParent()) {
            Rectangle bounds = parent.getBounds();

            dx += bounds.x;
            dy += bounds.y;
        }

        if (parent != null) {
            aRect.x += dx;
            aRect.y += dy;

            ((JComponent) parent).scrollRectToVisible(aRect);
            aRect.x -= dx;
            aRect.y -= dy;
        }
    }
}

Related

  1. scrollRectLater(final JComponent comp, final Rectangle r)
  2. scrollRectToVisible(Component component, Rectangle aRect)
  3. scrollToComponent(Component cmp)
  4. scrollToVisible(int row, int col)
  5. scrollV(String title, JComponent content)
  6. scrollView(Component component)