Example usage for javax.swing JViewport scrollRectToVisible

List of usage examples for javax.swing JViewport scrollRectToVisible

Introduction

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

Prototype

public void scrollRectToVisible(Rectangle contentRect) 

Source Link

Document

Scrolls the view so that Rectangle within the view becomes visible.

Usage

From source file:Main.java

public static void scrollToVisible(JTable table, int rowIndex, int vColIndex) {
    if (!(table.getParent() instanceof JViewport)) {
        return;//from ww  w . j a v a  2s.  c om
    }
    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);
    viewport.scrollRectToVisible(rect);
}

From source file:Main.java

public static void scrollToCenter(JTable table, int rowIndex, int vColIndex) {
    if (!(table.getParent() instanceof JViewport)) {
        return;//from w  w  w. j a  v  a 2  s  .c om
    }
    JViewport viewport = (JViewport) table.getParent();
    Rectangle rect = table.getCellRect(rowIndex, vColIndex, true);
    Rectangle viewRect = viewport.getViewRect();
    rect.setLocation(rect.x - viewRect.x, rect.y - viewRect.y);

    int centerX = (viewRect.width - rect.width) / 2;
    int centerY = (viewRect.height - rect.height) / 2;
    if (rect.x < centerX) {
        centerX = -centerX;
    }
    if (rect.y < centerY) {
        centerY = -centerY;
    }
    rect.translate(centerX, centerY);
    viewport.scrollRectToVisible(rect);
}

From source file:JTableHelper.java

public static void scrollToCenter(JTable table, int rowIndex, int vColIndex) {
    if (!(table.getParent() instanceof JViewport)) {
        return;/*from  w ww .  jav a2s  . co m*/
    }
    JViewport viewport = (JViewport) table.getParent();
    Rectangle rect = table.getCellRect(rowIndex, vColIndex, true);
    Rectangle viewRect = viewport.getViewRect();
    rect.setLocation(rect.x - viewRect.x, rect.y - viewRect.y);

    int centerX = (viewRect.width - rect.width) / 2;
    int centerY = (viewRect.height - rect.height) / 2;

    if (rect.x < centerX) {
        centerX = -centerX;
    }
    if (rect.y < centerY) {
        centerY = -centerY;
    }
    rect.translate(centerX, centerY);
    viewport.scrollRectToVisible(rect);
}

From source file:Main.java

/** 
 * Assumes table is contained in a JScrollPane.
 * Scrolls the cell (rowIndex, vColIndex) so that it is visible
 * within the viewport.//from  w w w. ja  v  a  2 s.com
 */
public static void scrollToVisible(JTable table, int row, int col) {
    if (!(table.getParent() instanceof JViewport))
        return;

    JViewport viewport = (JViewport) table.getParent();

    // This rectangle is relative to the table where the
    // northwest corner of cell (0,0) is always (0,0).
    Rectangle rect = table.getCellRect(row, col, true);

    // The location of the viewport relative to the table
    Point pt = viewport.getViewPosition();

    // Translate the cell location so that it is relative
    // to the view, assuming the northwest corner of the
    // view is (0,0)
    rect.setLocation(rect.x - pt.x, rect.y - pt.y);

    // Scroll the area into view
    viewport.scrollRectToVisible(rect);
}

From source file:com.igormaznitsa.sciareto.ui.editors.MMDEditor.java

@Override
public void onEnsureVisibilityOfTopic(@Nonnull final MindMapPanel source, @Nullable final Topic topic) {
    SwingUtilities.invokeLater(new Runnable() {

        @Override/* w  ww  .ja  v  a  2s.c o  m*/
        public void run() {
            if (topic == null) {
                return;
            }

            final AbstractElement element = (AbstractElement) topic.getPayload();
            if (element == null) {
                return;
            }

            final Rectangle2D orig = element.getBounds();
            final int GAP = 30;

            final Rectangle bounds = orig.getBounds();
            bounds.setLocation(Math.max(0, bounds.x - GAP), Math.max(0, bounds.y - GAP));
            bounds.setSize(bounds.width + GAP * 2, bounds.height + GAP * 2);

            final JViewport viewport = getViewport();
            final Rectangle visible = viewport.getViewRect();

            if (visible.contains(bounds)) {
                return;
            }

            bounds.setLocation(bounds.x - visible.x, bounds.y - visible.y);

            viewport.scrollRectToVisible(bounds);
        }

    });
}

From source file:net.sf.jabref.gui.maintable.MainTable.java

public void scrollToCenter(int rowIndex, int vColIndex) {
    if (!(this.getParent() instanceof JViewport)) {
        return;//from  w  w w. ja  v  a  2s  .  c o m
    }

    JViewport viewport = (JViewport) this.getParent();

    // This rectangle is relative to the table where the
    // northwest corner of cell (0,0) is always (0,0).
    Rectangle rect = this.getCellRect(rowIndex, vColIndex, true);

    // The location of the view relative to the table
    Rectangle viewRect = viewport.getViewRect();

    // Translate the cell location so that it is relative
    // to the view, assuming the northwest corner of the
    // view is (0,0).
    rect.setLocation(rect.x - viewRect.x, rect.y - viewRect.y);

    // Calculate location of rect if it were at the center of view
    int centerX = (viewRect.width - rect.width) / 2;
    int centerY = (viewRect.height - rect.height) / 2;

    // Fake the location of the cell so that scrollRectToVisible
    // will move the cell to the center
    if (rect.x < centerX) {
        centerX = -centerX;
    }
    if (rect.y < centerY) {
        centerY = -centerY;
    }
    rect.translate(centerX, centerY);

    // Scroll the area into view.
    viewport.scrollRectToVisible(rect);

    revalidate();
    repaint();
}

From source file:org.nuclos.client.ui.collect.result.SearchResultStrategy.java

private void adjustVerticalScrollBarForSearch(boolean bRefreshOnly) {
    final CollectController<Clct> cc = getCollectController();
    final JViewport viewport = cc.getResultPanel().getResultTableScrollPane().getViewport();
    if (bRefreshOnly) {
        final Rectangle rect = cc.getResultTable().getCellRect(0, 0, true);
        final Rectangle viewRect = viewport.getViewRect();
        // There seem to be different opinions about what scrollRectToVisible has to do at SUN and everywhere else...
        rect.setLocation(viewRect.x, viewRect.y);//rect.x - viewRect.x, rect.y - viewRect.y);
        viewport.scrollRectToVisible(rect);
    } else {//from  ww  w  .j a va2  s.c  o m
        Point viewPosition = viewport.getViewPosition();
        viewport.setViewPosition(new Point(viewPosition.x, 0));
    }
    final JScrollBar scrlbarVertical = cc.getResultPanel().getResultTableScrollPane().getVerticalScrollBar();
    scrlbarVertical.setValue(scrlbarVertical.getMinimum());
}

From source file:org.yccheok.jstock.gui.JTableUtilities.java

public static void scrollToVisible(JTable table, int rowIndex, int vColIndex) {
    if (!(table.getParent() instanceof JViewport)) {
        return;//from  ww  w  . ja  va 2  s  .  co  m
    }

    JViewport viewport = (JViewport) table.getParent();

    // This rectangle is relative to the table where the
    // northwest corner of cell (0,0) is always (0,0).
    Rectangle rect = table.getCellRect(rowIndex, vColIndex, true);

    // The location of the viewport relative to the table
    Point pt = viewport.getViewPosition();

    // Translate the cell location so that it is relative
    // to the view, assuming the northwest corner of the
    // view is (0,0)
    rect.setLocation(rect.x - pt.x, rect.y - pt.y);

    // Scroll the area into view
    viewport.scrollRectToVisible(rect);
}

From source file:xtrememp.PlaylistManager.java

public void makeRowVisible(int rowIndex) {
    if (!(playlistTable.getParent() instanceof JViewport)) {
        return;//from  w  w  w. ja v a2s.co m
    }
    JViewport viewport = (JViewport) playlistTable.getParent();
    Rectangle contentRect = (Rectangle) playlistTable
            .getCellRect(rowIndex, playlistTable.getSelectedColumn(), true).clone();
    Point pt = viewport.getViewPosition();
    contentRect.setLocation(contentRect.x - pt.x, contentRect.y - pt.y);
    viewport.scrollRectToVisible(contentRect);
}