Java JScrollPane Scroll scrollToVisible(int row, int col)

Here you can find the source of scrollToVisible(int row, int col)

Description

Assumes table is contained in a JScrollPane.

License

Open Source License

Declaration

public void scrollToVisible(int row, int col) 

Method Source Code

//package com.java2s;
/*/*from ww  w  . j a v  a 2  s .  co  m*/
 *    This program is free software; you can redistribute it and/or modify
 *    it under the terms of the GNU General Public License as published by
 *    the Free Software Foundation; either version 2 of the License, or
 *    (at your option) any later version.
 *
 *    This program is distributed in the hope that it will be useful,
 *    but WITHOUT ANY WARRANTY; without even the implied warranty of
 *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *    GNU General Public License for more details.
 *
 *    You should have received a copy of the GNU General Public License
 *    along with this program; if not, write to the Free Software
 *    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 */

import javax.swing.*;

import java.awt.*;

public class Main {
    private JTable jtable;

    /** 
     * Assumes table is contained in a JScrollPane.
     * Scrolls the cell (rowIndex, vColIndex) so that it is visible
     * within the viewport.
     */
    public void scrollToVisible(int row, int col) {
        scrollToVisible(getJTable(), row, col);
    }

    /** 
     * Assumes table is contained in a JScrollPane.
     * Scrolls the cell (rowIndex, vColIndex) so that it is visible
     * within the viewport.
     */
    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);
    }

    /**
     * returns the JTable
     */
    public JTable getJTable() {
        return jtable;
    }
}

Related

  1. scrolleable(final JComponent comp)
  2. scrollH(JComponent content)
  3. scrollRectLater(final JComponent comp, final Rectangle r)
  4. scrollRectToVisible(Component component, Rectangle aRect)
  5. scrollToComponent(Component cmp)
  6. scrollV(String title, JComponent content)
  7. scrollVertically(JComponent c, Rectangle r)
  8. scrollView(Component component)