Example usage for javax.swing JComponent scrollRectToVisible

List of usage examples for javax.swing JComponent scrollRectToVisible

Introduction

In this page you can find the example usage for javax.swing JComponent scrollRectToVisible.

Prototype

public void scrollRectToVisible(Rectangle aRect) 

Source Link

Document

Forwards the scrollRectToVisible() message to the JComponent's parent.

Usage

From source file:Main.java

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

    if (visible.x <= from && visible.x + visible.width >= to)
        return;//from  ww  w  .  j a v a2  s  . c  o  m

    visible.x = from;
    visible.width = to - from;

    c.scrollRectToVisible(visible);
}

From source file:Main.java

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

    if (visible.y <= from && visible.y + visible.height >= to)
        return;//w w w.  j a v a 2 s .  c o  m

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

    c.scrollRectToVisible(visible);
}

From source file:Main.java

public static void centerHorizontally(JComponent c, int from, int to, boolean withInsets) {
    Rectangle bounds = c.getBounds();
    Insets i = withInsets ? EMPTY_INSETS : c.getInsets();
    bounds.x = i.left;//w  ww . j  a v a 2  s . co m
    bounds.y = i.top;
    bounds.width -= i.left + i.right;
    bounds.height -= i.top + i.bottom;

    Rectangle visible = c.getVisibleRect();

    visible.x = from - (visible.width + from - to) / 2;

    if (visible.x < bounds.x)
        visible.x = bounds.x;

    if (visible.x + visible.width > bounds.x + bounds.width)
        visible.x = bounds.x + bounds.width - visible.width;

    c.scrollRectToVisible(visible);
}

From source file:Main.java

public static void centerVertically(JComponent c, int from, int to, boolean withInsets) {
    Rectangle bounds = c.getBounds();
    Insets i = withInsets ? EMPTY_INSETS : c.getInsets();
    bounds.x = i.left;//  w ww . j a va  2 s  .  c  om
    bounds.y = i.top;
    bounds.width -= i.left + i.right;
    bounds.height -= i.top + i.bottom;

    Rectangle visible = c.getVisibleRect();

    visible.y = from - (visible.height + from - to) / 2;

    if (visible.y < bounds.y)
        visible.y = bounds.y;

    if (visible.y + visible.height > bounds.y + bounds.height)
        visible.y = bounds.y + bounds.height - visible.height;

    c.scrollRectToVisible(visible);
}

From source file:Main.java

public static void center(JComponent c, Rectangle r, boolean withInsets) {
    Rectangle visible = c.getVisibleRect();

    visible.x = r.x - (visible.width - r.width) / 2;
    visible.y = r.y - (visible.height - r.height) / 2;

    Rectangle bounds = c.getBounds();
    Insets i = withInsets ? EMPTY_INSETS : c.getInsets();
    bounds.x = i.left;//  w ww.  jav a  2s .co  m
    bounds.y = i.top;
    bounds.width -= i.left + i.right;
    bounds.height -= i.top + i.bottom;

    if (visible.x < bounds.x)
        visible.x = bounds.x;

    if (visible.x + visible.width > bounds.x + bounds.width)
        visible.x = bounds.x + bounds.width - visible.width;

    if (visible.y < bounds.y)
        visible.y = bounds.y;

    if (visible.y + visible.height > bounds.y + bounds.height)
        visible.y = bounds.y + bounds.height - visible.height;

    c.scrollRectToVisible(visible);
}

From source file:Main.java

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

    dest.x = from;//from w  w w  .j  a  v  a  2  s  .c  om
    dest.width = to - from;

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

            dest.width = visible.width;
        }
    }

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

From source file:Main.java

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

    dest.y = from;//  w  w w  .  ja v  a  2  s. c  om
    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);
}