Java JTable Column Width Set autoResizeColWidth(JTable table, int extraSpaceToColumn)

Here you can find the source of autoResizeColWidth(JTable table, int extraSpaceToColumn)

Description

auto Resize Col Width

License

Open Source License

Declaration

public static JTable autoResizeColWidth(JTable table, int extraSpaceToColumn) 

Method Source Code


//package com.java2s;
// Licensed under the Apache License, Version 2.0 (the "License"); you may

import javax.swing.*;

import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.DefaultTableColumnModel;
import javax.swing.table.TableCellRenderer;
import javax.swing.table.TableColumn;
import java.awt.*;

public class Main {
    public static JTable autoResizeColWidth(JTable table) {
        int totalWidth = autoResizeColWidthNoFill(table);

        double availableWidth = table.getParent().getWidth();

        if (totalWidth >= availableWidth) {
            return table;
        }//from   w w w  . j  a v a  2 s .com
        double increaseBy = availableWidth / (double) totalWidth;
        int newTotalWidth = 0;
        for (int i = 0; i < table.getColumnCount(); i++) {
            DefaultTableColumnModel colModel = (DefaultTableColumnModel) table.getColumnModel();
            TableColumn col = colModel.getColumn(i);
            int oldWidth = col.getPreferredWidth();
            int newWidth = oldWidth;
            if (i < table.getColumnCount() - 1) {
                // All but the last column
                double width = (double) newWidth;
                newWidth = (int) (width * increaseBy);
            } else {
                // Last column. Make up the diff.
                newWidth = (int) availableWidth - newTotalWidth;
            }

            // Set the width
            col.setPreferredWidth(newWidth);
            newTotalWidth += newWidth;
        }

        return table;
    }

    public static JTable autoResizeColWidth(JTable table, int extraSpaceToColumn) {
        if (extraSpaceToColumn < 0 || extraSpaceToColumn >= table.getColumnCount()) {
            throw new IllegalArgumentException("Illegal Column index.  Table " + table.getName() + " has "
                    + table.getColumnCount() + " columns.  Can't set extra space to column " + extraSpaceToColumn);
        }

        int totalWidth = autoResizeColWidthNoFill(table);

        int availableWidth = table.getParent().getWidth();

        if (totalWidth >= availableWidth) {
            return table;
        }
        int increaseBy = availableWidth - totalWidth;

        DefaultTableColumnModel colModel = (DefaultTableColumnModel) table.getColumnModel();
        TableColumn col = colModel.getColumn(extraSpaceToColumn);
        int oldWidth = col.getPreferredWidth();
        col.setPreferredWidth(oldWidth + increaseBy);

        return table;
    }

    public static int autoResizeColWidthNoFill(JTable table) {
        int totalWidth = 0;
        int headerWidth = 0;

        table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);

        int margin = 5;
        for (int i = 0; i < table.getColumnCount(); i++) {
            int vColIndex = i;
            DefaultTableColumnModel colModel = (DefaultTableColumnModel) table.getColumnModel();
            TableColumn col = colModel.getColumn(vColIndex);
            int width = 0;

            // Get width of column header
            TableCellRenderer renderer = col.getHeaderRenderer();

            if (renderer == null) {
                renderer = table.getTableHeader().getDefaultRenderer();
            }

            Component comp = renderer.getTableCellRendererComponent(table, col.getHeaderValue(), false, false, 0,
                    0);

            width = comp.getPreferredSize().width;
            headerWidth = width;

            // Get maximum width of column data
            for (int r = 0; r < table.getRowCount(); r++) {
                renderer = table.getCellRenderer(r, vColIndex);
                comp = renderer.getTableCellRendererComponent(table, table.getValueAt(r, vColIndex), false, false,
                        r, vColIndex);
                width = Math.max(width, comp.getPreferredSize().width);
            }

            // Add margin
            width += 2 * margin;

            width = Math.max(width, headerWidth + 20); // Make sure the width is wider than the
                                                       // header plus 20 to leave room for sorting
                                                       // arrows.

            // Set the width
            col.setPreferredWidth(width);

            totalWidth += width;
        }

        ((DefaultTableCellRenderer) table.getTableHeader().getDefaultRenderer())
                .setHorizontalAlignment(SwingConstants.LEFT);

        // table.setAutoCreateRowSorter(true);
        table.getTableHeader().setReorderingAllowed(false);

        return totalWidth;
    }
}

Related

  1. adjustColumnWidth(TableModel model, int columnIndex, int maxWidth, int rightPadding, JTable table)
  2. adjustColWidth(JTable table, int firstColumnWidth, int middleColumnWidth, int lastColumnWidth, int padding)
  3. autoFitColumnWidth(JTable table, TableColumn tableColumn)
  4. autoResizeColWidth(final JTable table, final DefaultTableModel model)
  5. autoResizeColWidth(JTable table, AbstractTableModel model)
  6. autoResizeColWidthNoFill(JTable table)
  7. calcColumnWidth(int col)
  8. chooseGoodColumnWidths(JTable table)
  9. fitColumnWidth(JTable table, int colIndex, int padding)