Java JTable Scroll scrollToLastSelectedRow(final JTable table)

Here you can find the source of scrollToLastSelectedRow(final JTable table)

Description

scroll To Last Selected Row

License

Open Source License

Declaration

public static void scrollToLastSelectedRow(final JTable table) 

Method Source Code


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

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

public class Main {
    public static void scrollToLastSelectedRow(final JTable table) {
        if (table == null) {
            return;
        }/* w w w .  j  av  a2 s.  c o  m*/
        int[] selectedRows = table.getSelectedRows();
        if (selectedRows.length > 0) {
            scrollToVisible(table, selectedRows[selectedRows.length - 1], 0);
        }
    }

    public static void scrollToVisible(final JTable table, int rowIndex, int vColIndex) {
        if (table == null || !(table.getParent() instanceof JViewport)) {
            return;
        }
        JViewport viewport = (JViewport) table.getParent();

        try {
            final Rectangle rect = table.getCellRect(rowIndex, vColIndex, true);
            Point pt = viewport.getViewPosition();
            rect.setLocation(rect.x - pt.x, rect.y - pt.y);
            SwingUtilities.invokeLater(new Runnable() {
                @Override
                public void run() {
                    table.scrollRectToVisible(rect);
                }
            });
        } catch (Exception e) {
        }
    }
}

Related

  1. findScrollPane(JTable p_Table)
  2. getScrollPane(JTable myTable)
  3. makeTransparant(JTable table, JScrollPane scrollPane)
  4. scroll(JTable table, int rowIndex, int vColIndex)
  5. scrollToCenter(JTable table, int rowIndex, int vColIndex)
  6. scrollToPosition(JTable table, int insertRow)
  7. scrollToRow(JTable table, int row)
  8. scrollToSelectedRow(JTable table)
  9. scrollToTableCell(JTable table, int rowIndex, int colIndex)