recalculate Scrollable JTable Viewport Size - Java Swing

Java examples for Swing:JTable Scroll

Description

recalculate Scrollable JTable Viewport Size

Demo Code


//package com.java2s;
import java.awt.Dimension;

import javax.swing.JTable;

public class Main {
    public static void recalculateScrollableViewportSize(JTable table,
            int minScrollableViewPortHeight, int maxScrollableViewPortHeight) {
        int requiredHeight = table.getModel().getRowCount()
                * table.getRowHeight();//from w w w . ja  va  2  s . co m
        requiredHeight = Math.max(requiredHeight,
                minScrollableViewPortHeight);
        requiredHeight = Math.min(requiredHeight,
                maxScrollableViewPortHeight);

        table.setMaximumSize(new Dimension(table
                .getPreferredScrollableViewportSize().width, requiredHeight));
        table.setMinimumSize(new Dimension(table
                .getPreferredScrollableViewportSize().width, requiredHeight));
        table.setPreferredScrollableViewportSize(new Dimension(table
                .getPreferredScrollableViewportSize().width, requiredHeight));

        if (table.getTopLevelAncestor() != null)
            table.getTopLevelAncestor().repaint();
    }
}

Related Tutorials