Java JTable Scroll scroll(JTable table, int rowIndex, int vColIndex)

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

Description

Scrolls view if it is located in a JViewport, so that the specified cell is displayed in the center.

License

Apache License

Declaration

public static void scroll(JTable table, int rowIndex, int vColIndex) 

Method Source Code

//package com.java2s;
/*****************************************************************
 *   Licensed to the Apache Software Foundation (ASF) under one
 *  or more contributor license agreements.  See the NOTICE file
 *  distributed with this work for additional information
 *  regarding copyright ownership.  The ASF licenses this file
 *  to you 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.//w  w  w  .ja v  a  2s  .  c  o  m
 ****************************************************************/

import javax.swing.*;
import java.awt.*;

public class Main {
    /**
    * Scrolls view if it is located in a JViewport, so that the specified cell
    * is displayed in the center.
    */
    public static void scroll(JTable table, int rowIndex, int vColIndex) {
        if (!(table.getParent() instanceof JViewport)) {
            return;
        }

        JViewport viewport = (JViewport) table.getParent();
        Rectangle rect = table.getCellRect(rowIndex, vColIndex, true);
        Rectangle viewRect = viewport.getViewRect();

        if (viewRect.intersects(rect)) {
            return;
        }

        // 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 - viewRect.x, rect.y - viewRect.y);

        // Calculate location of rect if it were at the center of view
        int centerX = (viewRect.width - rect.width) / 2;
        int centerY = (viewRect.height - rect.height) / 2;

        // Fake the location of the cell so that scrollRectToVisible
        // will move the cell to the center
        if (rect.x < centerX) {
            centerX = -centerX;
        }
        if (rect.y < centerY) {
            centerY = -centerY;
        }
        rect.translate(centerX, centerY);

        // Scroll the area into view.
        viewport.scrollRectToVisible(rect);
    }
}

Related

  1. AutoScrollTable(final JTable table_packets)
  2. createTableScrollPane(final JTable table)
  3. findScrollPane(JTable p_Table)
  4. getScrollPane(JTable myTable)
  5. makeTransparant(JTable table, JScrollPane scrollPane)
  6. scrollToCenter(JTable table, int rowIndex, int vColIndex)
  7. scrollToLastSelectedRow(final JTable table)
  8. scrollToPosition(JTable table, int insertRow)
  9. scrollToRow(JTable table, int row)