Example usage for javax.swing JViewport getViewSize

List of usage examples for javax.swing JViewport getViewSize

Introduction

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

Prototype

public Dimension getViewSize() 

Source Link

Document

If the view's size hasn't been explicitly set, return the preferred size, otherwise return the view's current size.

Usage

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  w  w w. ja 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  w ww . jav a 2s . co 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);
}