Java JTable Header getMaxColumnWidth(JTable aTable, int columnNo, boolean includeColumnHeaderWidth, int columnPadding)

Here you can find the source of getMaxColumnWidth(JTable aTable, int columnNo, boolean includeColumnHeaderWidth, int columnPadding)

Description

get Max Column Width

License

Apache License

Declaration

private static int getMaxColumnWidth(JTable aTable, int columnNo,
            boolean includeColumnHeaderWidth, int columnPadding) 

Method Source Code

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

import javax.swing.*;

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

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

public class Main {
    private static int getMaxColumnWidth(JTable aTable, int columnNo,
            boolean includeColumnHeaderWidth, int columnPadding) {
        TableColumn column = aTable.getColumnModel().getColumn(columnNo);
        Component comp = null;/*from www .  j  a  va2s . c o m*/
        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. calcHeaderWidth(JTable table, int col)
  2. cleanHeaderCells(JTable table)
  3. createTableHeaderEmptyColumnPainter(final JTable table)
  4. getColumnHeaderWidth(TableColumn col, JTable table)
  5. getHeaderDimension(JTable table, TableColumn tableColumn)
  6. getSelectedColumn(JTableHeader tableHeader)
  7. getTableHeaderBackground()
  8. getTableHeaderColor()
  9. headerString(JTable t)