Java JTable Scroll scrollToVisible(JTable table, int row, int col)

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

Description

Assumes table is contained in a JScrollPane.

License

Open Source License

Declaration

public static void scrollToVisible(JTable table, int row, int col) 

Method Source Code

//package com.java2s;
/*//www  .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 java.awt.Point;
import java.awt.Rectangle;
import javax.swing.JTable;
import javax.swing.JViewport;

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. scrollToTableCell(JTable table, int rowIndex, int colIndex)
  2. scrollToVisible(final JTable table, final int rowIndex, final int vColIndex)
  3. scrollToVisible(final JTable table, final int rowIndex, final int vColIndex)
  4. scrollToVisible(final JTable table, int rowIndex, int vColIndex)
  5. scrollToVisible(JTable table, int row, int col)
  6. scrollToVisible(JTable table, int rowIndex, int colIndex)
  7. scrollToVisible(JTable table, int rowIndex, int colIndex, boolean center)
  8. scrollToVisible(JTable table, int rowIndex, int vColIndex)
  9. scrollToVisible(JTable table, int rowIndex, int vColIndex)