Java JTable Row Visible setVisibleRowCount(final JTable table, final int rows)

Here you can find the source of setVisibleRowCount(final JTable table, final int rows)

Description

Set the preferred viewport size on a table based upon the number of rows that should be visible and the current heights of the rows.

License

Open Source License

Parameter

Parameter Description
table the table
rows number of rows to have visible without scrolling

Declaration

public static void setVisibleRowCount(final JTable table, final int rows) 

Method Source Code

//package com.java2s;

import java.awt.Dimension;

import javax.swing.JTable;

public class Main {
    /**/*w  ww.j  av a  2  s .  c  om*/
     * Set the preferred viewport size on a table based upon the number of rows
     * that should be visible and the current heights of the rows. Based upon code
     * from http://www.javalobby.org/java/forums/t19559.html
     * 
     * @param table the table
     * @param rows number of rows to have visible without scrolling
     */
    public static void setVisibleRowCount(final JTable table, final int rows) {
        int height = 0;
        for (int row = 0; row < rows; row++) {
            height += table.getRowHeight(row);
        }

        table.setPreferredScrollableViewportSize(
                new Dimension(table.getPreferredScrollableViewportSize().width, height));
    }
}

Related

  1. getLeadingRow(JTable table, Rectangle visibleRect)
  2. getVisibleRowCount(JTable list)
  3. makeRowVisible(JTable table, int row)
  4. setupTableUI(JTable table, int visibleRows)
  5. setVisibleRowCount(final JTable table, final int rows)