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

Parameter

Parameter Description
table the table to work with
row the row index
col the column index

Declaration

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

Method Source Code

//package com.java2s;
/*//from w ww.jav  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 3 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, see <http://www.gnu.org/licenses/>.
 */

import javax.swing.*;

import java.awt.*;

public class Main {
    /** the table to work with. */
    protected JTable m_Table;

    /**
     * Assumes table is contained in a JScrollPane.
     * Scrolls the cell (rowIndex, vColIndex) so that it is visible
     * within the viewport.
     *
     * @param row      the row index
     * @param col      the column index
     */
    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.
     *
     * @param table   the table to work with
     * @param row      the row index
     * @param col      the column index
     */
    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.
     *
     * @return      the table to work on
     */
    public JTable getJTable() {
        return m_Table;
    }
}

Related

  1. scrollToSelectedRow(JTable table)
  2. scrollToTableCell(JTable table, int rowIndex, int colIndex)
  3. scrollToVisible(final JTable table, final int rowIndex, final int vColIndex)
  4. scrollToVisible(final JTable table, final int rowIndex, final int vColIndex)
  5. scrollToVisible(final JTable table, int rowIndex, int vColIndex)
  6. scrollToVisible(JTable table, int row, int col)
  7. scrollToVisible(JTable table, int rowIndex, int colIndex)
  8. scrollToVisible(JTable table, int rowIndex, int colIndex, boolean center)
  9. scrollToVisible(JTable table, int rowIndex, int vColIndex)