Example usage for javax.swing JComponent getVisibleRect

List of usage examples for javax.swing JComponent getVisibleRect

Introduction

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

Prototype

@BeanProperty(bound = false)
public Rectangle getVisibleRect() 

Source Link

Document

Returns the Component's "visible rectangle" - the intersection of this component's visible rectangle, new Rectangle(0, 0, getWidth(), getHeight()), and all of its ancestors' visible rectangles.

Usage

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;/*from  w w w  . ja v  a 2s.  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);
}