scroll JTable To Top And Select First - Java Swing

Java examples for Swing:JTable Scroll

Description

scroll JTable To Top And Select First

Demo Code


//package com.java2s;

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

public class Main {
    public static void scrollToTopAndSelectFirst(JTable table) {
        synchronized (table.getModel()) {
            if (!(table.getParent() instanceof JViewport)) {
                return;
            }//from w  ww  . ja v a  2s . c  o m
            table.scrollRectToVisible(new Rectangle(0, 0, 0, table
                    .getRowHeight()));
            // select first
            if (table.getModel().getRowCount() > 0) {
                table.getSelectionModel().setSelectionInterval(0, 0);
            }
        }
    }
}

Related Tutorials