Java JTable Auto Resize autoResizeTable(JTable aTable, int columnPadding)

Here you can find the source of autoResizeTable(JTable aTable, int columnPadding)

Description

auto Resize Table

License

Apache License

Declaration

public static int autoResizeTable(JTable aTable, int columnPadding) 

Method Source Code

//package com.java2s;
//License from project: Apache License 

import javax.swing.*;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Component;
import javax.swing.text.JTextComponent;

import javax.swing.table.JTableHeader;
import javax.swing.table.TableColumnModel;
import javax.swing.table.TableColumn;
import javax.swing.table.TableCellRenderer;

public class Main {
    private static final int DEFAULT_COLUMN_PADDING = 5;

    public static int autoResizeTable(JTable aTable) {
        return autoResizeTable(aTable, DEFAULT_COLUMN_PADDING);
    }/*from   ww w . ja  va  2s .c o m*/

    public static int autoResizeTable(JTable aTable, int columnPadding) {
        int columnCount = aTable.getColumnCount();
        int currentTableWidth = aTable.getWidth();
        int tableWidth = 0;
        Dimension cellSpacing = aTable.getIntercellSpacing();
        if (columnCount > 0) // must have columns !
        {
            // STEP ONE : Work out the column widths
            int columnWidth[] = new int[columnCount];
            for (int i = 0; i < columnCount; i++) {
                columnWidth[i] = getMaxColumnWidth(aTable, i, true,
                        columnPadding);
                tableWidth += columnWidth[i];
            }
            // account for cell spacing too
            tableWidth += (columnCount - 1) * cellSpacing.width;
            // STEP TWO : Dynamically resize each column
            // try changing the size of the column names area
            JTableHeader tableHeader = aTable.getTableHeader();
            Dimension headerDim = tableHeader.getPreferredSize();
            // headerDim.height = tableHeader.getHeight();
            headerDim.width = tableWidth;
            tableHeader.setPreferredSize(headerDim); // Header only set width

            Dimension tableDim = new Dimension(); // For seting the whole table
            int rowHeight = aTable.getRowHeight();
            if (rowHeight == 0)
                rowHeight = 16; // default rowheight
            //System.out.println("Row Height : " + rowHeight);
            //tableDim.height = headerDim.height + (rowHeight + cellSpacing.height) * aTable.getRowCount();
            tableDim.width = tableWidth;
            //System.out.println("AutofitTableColumns.autoResizeTable() - Setting Table size to ( " + tableDim.width + ", " + tableDim.height + " )" );
            //aTable.setPreferredSize(tableDim);
            TableColumnModel tableColumnModel = aTable.getColumnModel();
            TableColumn tableColumn;
            for (int i = 0; i < columnCount; i++) {
                tableColumn = tableColumnModel.getColumn(i);
                tableColumn.setPreferredWidth(columnWidth[i]);
            }
            aTable.invalidate();
            aTable.doLayout();
            aTable.repaint();
        }
        return tableWidth;
    }

    private static int getMaxColumnWidth(JTable aTable, int columnNo,
            boolean includeColumnHeaderWidth, int columnPadding) {
        TableColumn column = aTable.getColumnModel().getColumn(columnNo);
        Component comp = null;
        int maxWidth = 0;
        if (includeColumnHeaderWidth) {
            TableCellRenderer headerRenderer = column.getHeaderRenderer();
            if (headerRenderer != null) {
                comp = headerRenderer.getTableCellRendererComponent(aTable,
                        column.getHeaderValue(), false, false, 0, columnNo);
                if (comp instanceof JTextComponent) {
                    JTextComponent jtextComp = (JTextComponent) comp;
                    String text = jtextComp.getText();
                    Font font = jtextComp.getFont();
                    FontMetrics fontMetrics = jtextComp
                            .getFontMetrics(font);
                    maxWidth = SwingUtilities.computeStringWidth(
                            fontMetrics, text);
                } else {
                    maxWidth = comp.getPreferredSize().width;
                }
            } else {
                try {
                    String headerText = (String) column.getHeaderValue();
                    JLabel defaultLabel = new JLabel(headerText);
                    Font font = defaultLabel.getFont();
                    FontMetrics fontMetrics = defaultLabel
                            .getFontMetrics(font);
                    maxWidth = SwingUtilities.computeStringWidth(
                            fontMetrics, headerText);
                } catch (ClassCastException ce) {
                    // Can't work out the header column width..
                    maxWidth = 0;
                }
            }
        }
        TableCellRenderer tableCellRenderer;
        // Component comp;
        int cellWidth = 0;
        for (int i = 0; i < aTable.getRowCount(); i++) {
            tableCellRenderer = aTable.getCellRenderer(i, columnNo);
            comp = tableCellRenderer.getTableCellRendererComponent(aTable,
                    aTable.getValueAt(i, columnNo), false, false, i,
                    columnNo);
            if (comp instanceof JTextComponent) {
                JTextComponent jtextComp = (JTextComponent) comp;
                String text = jtextComp.getText();
                Font font = jtextComp.getFont();
                FontMetrics fontMetrics = jtextComp.getFontMetrics(font);
                int textWidth = SwingUtilities.computeStringWidth(
                        fontMetrics, text);
                maxWidth = Math.max(maxWidth, textWidth);
            } else {
                cellWidth = comp.getPreferredSize().width;
                // maxWidth = Math.max ( headerWidth, cellWidth );
                maxWidth = Math.max(maxWidth, cellWidth);
            }
        }
        return (maxWidth + columnPadding);
    }
}

Related

  1. autoResizeTableColWidths(JTable table)
  2. autoResizeTableRowHeights(JTable table)