Example usage for javax.swing JViewport getExtentSize

List of usage examples for javax.swing JViewport getExtentSize

Introduction

In this page you can find the example usage for javax.swing JViewport getExtentSize.

Prototype

@Transient
public Dimension getExtentSize() 

Source Link

Document

Returns the size of the visible part of the view in view coordinates.

Usage

From source file:Main.java

/**
 * @return the visible size of a component in its viewport (including
 *         scrollbars)//from  www.  ja  v a2 s.com
 */
public static Dimension getVisibleSizeInViewport(Component c) {
    if (c.getParent() instanceof JViewport) {
        JViewport vp = (JViewport) c.getParent();
        Dimension d = vp.getExtentSize();
        if (vp.getParent() instanceof JScrollPane) {
            JScrollPane sp = (JScrollPane) vp.getParent();
            if (sp.getVerticalScrollBar() != null && sp.getVerticalScrollBar().isVisible()) {
                d.width += sp.getVerticalScrollBar().getWidth();
            }
            if (sp.getHorizontalScrollBar() != null && sp.getHorizontalScrollBar().isVisible()) {
                d.height += sp.getHorizontalScrollBar().getHeight();
            }
        }
        return d;
    } else {
        return null;
    }
}

From source file:Main.java

public static boolean isCellVisible(JTable table, int rowIndex, int vColIndex) {
    if (!(table.getParent() instanceof JViewport)) {
        return false;
    }//from w  w w .j  a  v a 2 s .c  o m
    JViewport viewport = (JViewport) table.getParent();
    Rectangle rect = table.getCellRect(rowIndex, vColIndex, true);
    Point pt = viewport.getViewPosition();
    rect.setLocation(rect.x - pt.x, rect.y - pt.y);
    return new Rectangle(viewport.getExtentSize()).contains(rect);
}

From source file:de.codesourcery.jasm16.utils.ASTInspector.java

public void centerCurrentLineInScrollPane() {
    final Container container = SwingUtilities.getAncestorOfClass(JViewport.class, editorPane);

    if (container == null) {
        return;/*from ww w. j  a  va2  s  .  c  om*/
    }

    try {
        final Rectangle r = editorPane.modelToView(editorPane.getCaretPosition());
        final JViewport viewport = (JViewport) container;
        final int extentHeight = viewport.getExtentSize().height;
        final int viewHeight = viewport.getViewSize().height;

        int y = Math.max(0, r.y - (extentHeight / 2));
        y = Math.min(y, viewHeight - extentHeight);

        viewport.setViewPosition(new Point(0, y));
    } catch (BadLocationException ble) {
    }
}

From source file:de.codesourcery.jasm16.ide.ui.views.SourceCodeView.java

public final void centerCurrentLineInScrollPane() {
    final Runnable r = new Runnable() {
        @Override//from  ww  w .  j  a  v  a  2s . c  o  m
        public void run() {

            final Container container = SwingUtilities.getAncestorOfClass(JViewport.class, editorPane);

            if (container == null) {
                return;
            }

            try {
                final Rectangle r = editorPane.modelToView(editorPane.getCaretPosition());
                if (r == null) {
                    return;
                }
                final JViewport viewport = (JViewport) container;
                final int extentHeight = viewport.getExtentSize().height;
                final int viewHeight = viewport.getViewSize().height;

                int y = Math.max(0, r.y - (extentHeight / 2));
                y = Math.min(y, viewHeight - extentHeight);

                viewport.setViewPosition(new Point(0, y));
            } catch (BadLocationException ble) {
                LOG.error("centerCurrentLineInScrollPane(): ", ble);
            }
        }

    };

    UIUtils.invokeLater(r);
}