Java JTable Scroll selectAndScroll(JTable table, int rowIndex)

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

Description

select And Scroll

License

Open Source License

Declaration

public static void selectAndScroll(JTable table, int rowIndex) 

Method Source Code

//package com.java2s;
/*//from w w w .j  av a 2 s  .c  o  m
 *     GENESE - Gerador de N?meros e Estat?sticas para Mega-Sena
 *     Copyright (C)  2016  Rafael Teixeira
 *     rafaelfst@live.com
 *
 *     GENESE ? um software livre: voc? pode redistribu?-lo e/ou modific?-lo
 *     dentro dos termos da Licen?a P?blica Geral GNU como publicada pela
 *     Funda??o do Software Livre (FSF), na vers?o 3 da Licen?a, ou
 *     (na sua opini?o) qualquer vers?o posterior.
 *
 *     Este programa ? distribu?do na esperan?a de que possa ser ?til,
 *     mas SEM NENHUMA GARANTIA; sem uma garantia impl?cita de ADEQUA??O
 *     a qualquer MERCADO ou APLICA??O EM PARTICULAR. Veja a
 *     Licen?a P?blica Geral GNU para maiores detalhes.
 *
 *     Voc? deve ter recebido uma c?pia da Licen?a P?blica Geral GNU junto
 *     com este programa. Se n?o, veja <http://www.gnu.org/licenses/>.
 */

import java.awt.Point;
import java.awt.Rectangle;
import javax.swing.JTable;

import javax.swing.JViewport;

public class Main {
    public static void selectAndScroll(JTable table, int rowIndex) {
        table.getSelectionModel().setSelectionInterval(rowIndex, rowIndex);
        scrollToVisible(table, rowIndex);
    }

    public static void scrollToVisible(JTable table, int rowIndex) {
        scrollToVisible(table, rowIndex, 0);
    }

    public static void scrollToVisible(JTable table, int rowIndex, int vColIndex) {
        if (!(table.getParent() instanceof JViewport))
            return;

        setViewPortPosition((JViewport) table.getParent(), table.getCellRect(rowIndex, vColIndex, true));
    }

    private static void setViewPortPosition(JViewport viewport, Rectangle position) {
        // The location of the viewport relative to the object
        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)
        position.setLocation(position.x - pt.x, position.y - pt.y);

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

Related

  1. scrollToVisible(JTable table, int rowIndex, int vColIndex)
  2. scrollToVisible(JTable table, int rowIndex, int vColIndex)
  3. scrollToVisible(JTable table, int rowIndex, int vColIndex)
  4. scrollToVisible(JTable table, int rowIndex, int vColIndex)
  5. scrollToVisible(JTable table, int rowIndex, int vColIndex)
  6. selectAndScrollToPosition(JTable table, int index0, int index1)
  7. selectRow(int row, JTable table, JScrollPane pane)
  8. selectRow(JTable table, int row, boolean bScroll)
  9. showMessageDialogInScrollableUneditableTextArea( java.awt.Component owner, String text, String title, int messageType, final int maxPreferredWidth, final int maxPreferredHeight)