Java JTable Scroll scrollToRow(JTable table, int row)

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

Description

scroll To Row

License

Apache License

Declaration

public static void scrollToRow(JTable table, int row) 

Method Source Code

//package com.java2s;
/*/* w  w  w . j av a 2  s  . c  o m*/
 * Copyright 2002-2004 the original author or authors.
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */

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

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

public class Main {
    public static void scrollToRow(JTable table, int row) {
        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, 0, 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);
    }
}

Related

  1. makeTransparant(JTable table, JScrollPane scrollPane)
  2. scroll(JTable table, int rowIndex, int vColIndex)
  3. scrollToCenter(JTable table, int rowIndex, int vColIndex)
  4. scrollToLastSelectedRow(final JTable table)
  5. scrollToPosition(JTable table, int insertRow)
  6. scrollToSelectedRow(JTable table)
  7. scrollToTableCell(JTable table, int rowIndex, int colIndex)
  8. scrollToVisible(final JTable table, final int rowIndex, final int vColIndex)
  9. scrollToVisible(final JTable table, final int rowIndex, final int vColIndex)