Java JTable Column FitTableColumns(JTable myTable)

Here you can find the source of FitTableColumns(JTable myTable)

Description

Fit Table Columns

License

Apache License

Declaration

public static void FitTableColumns(JTable myTable) 

Method Source Code

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

import java.util.Enumeration;
import javax.swing.JTable;

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

public class Main {

    public static void FitTableColumns(JTable myTable) {
        JTableHeader header = myTable.getTableHeader();
        int rowCount = myTable.getRowCount();
        Enumeration columns = myTable.getColumnModel().getColumns();
        while (columns.hasMoreElements()) {
            TableColumn column = (TableColumn) columns.nextElement();
            int col = header.getColumnModel().getColumnIndex(column.getIdentifier());
            int width = (int) myTable.getTableHeader().getDefaultRenderer()
                    .getTableCellRendererComponent(myTable, column.getIdentifier(), false, false, -1, col)
                    .getPreferredSize().getWidth();
            for (int row = 0; row < rowCount; row++) {
                int preferedWidth = (int) myTable.getCellRenderer(row, col).getTableCellRendererComponent(myTable,
                        myTable.getValueAt(row, col), false, false, row, col).getPreferredSize().getWidth();
                width = Math.max(width, preferedWidth);
            }/* w w  w .j  a v a2s.co  m*/
            header.setResizingColumn(column);
            column.setWidth(width + myTable.getIntercellSpacing().width);
        }
    }
}

Related

  1. ensureColumnCount(int count, JTable table)
  2. filterTable(JTable table, DefaultTableModel dtm, String text, int[] columns, boolean forceRegEx)
  3. findColumn(TableModel m, String columnName)
  4. findColumn(TableModel model, String name)
  5. fitColumns(JTable table)
  6. formatStringListToTable(List columnNames, List rows)
  7. getColumnData(final JTable table, final int iSelectedRow)
  8. getColumnIndex(JTable table, String columnTitle)
  9. getColumnIndexFromName(TableModel tableModel, String columnToRemove)