Java JTable Scroll scrollToVisible(final JTable table, final int rowIndex, final int vColIndex)

Here you can find the source of scrollToVisible(final JTable table, final int rowIndex, final int vColIndex)

Description

Ensure that a particular table cell is visible.

License

Open Source License

Parameter

Parameter Description
table the table
rowIndex the row index
vColIndex the column index

Declaration

public static void scrollToVisible(final JTable table, final int rowIndex, final int vColIndex) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

import java.awt.Point;
import java.awt.Rectangle;

import javax.swing.JTable;
import javax.swing.JViewport;

public class Main {
    /**/*ww  w.  j a  v  a  2 s. c  o  m*/
     * Ensure that a particular table cell is visible.
     * 
     * @param table the table
     * @param rowIndex the row index
     * @param vColIndex the column index
     */
    public static void scrollToVisible(final JTable table, final int rowIndex, final int vColIndex) {
        // Check the parent
        if (!(table.getParent() instanceof JViewport)) {
            return;
        }

        // Get the parent viewport
        JViewport viewport = (JViewport) table.getParent();

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

        // The location of the viewport relative to the table
        final 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);
    }
}

Related

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