Java Utililty Methods JTable Header

List of utility methods to do JTable Header

Description

The list of methods to do JTable Header are organized into topic(s).

Method

voidAddToTable(JTable tbl, Object[] obj, boolean head)
Add To Table
int size = tbl.getRowCount();
DefaultTableModel tableModel = (DefaultTableModel) tbl.getModel();
if (head) {
    tableModel.insertRow(0, obj);
} else {
    tableModel.insertRow(size, obj);
voidadjustHeader(JTable table)
adjust Header
TableCellRenderer headerRenderer = table.getTableHeader().getDefaultRenderer();
for (int i = 0; i < table.getColumnCount(); i++) {
    TableColumn column = table.getColumnModel().getColumn(i);
    Component header = headerRenderer.getTableCellRendererComponent(table, column.getHeaderValue(), false,
            false, 0, i);
    column.setPreferredWidth(header.getPreferredSize().width + 8);
intcalcHeaderWidth(int col)
calcs the optimal header width of the given column
return calcHeaderWidth(getJTable(), col);
intcalcHeaderWidth(JTable table, int col)
Calculates the optimal width for the header of the given table.
if (table == null)
    return -1;
if (col < 0 || col > table.getColumnCount()) {
    System.out.println("invalid col " + col);
    return -1;
JTableHeader header = table.getTableHeader();
TableCellRenderer defaultHeaderRenderer = null;
...
voidcleanHeaderCells(JTable table)
clean Header Cells
for (int cCnt = 0; cCnt < table.getColumnCount(); ++cCnt) {
    if (table.getValueAt(0, cCnt) == null) {
        table.setValueAt("", 0, cCnt);
    } else {
        String cellValue = (String) table.getValueAt(0, cCnt);
        if (cellValue.length() > 0) {
            for (int cnt = 0; cnt < badChars.length; ++cnt) {
                int indexValue = -1;
...
BordercreateTableHeaderEmptyColumnPainter(final JTable table)
Creates a Border that paints any empty space to the right of the last column header in the given JTable 's JTableHeader .
return new AbstractBorder() {
    @Override
    public void paintBorder(Component c, Graphics g, int x, int y, int width, int height) {
        JViewport viewport = (JViewport) table.getParent();
        if (viewport != null && table.getWidth() < viewport.getWidth()) {
            int startX = table.getWidth();
            int emptyColumnWidth = viewport.getWidth() - table.getWidth();
            paintHeader(g, table, startX, emptyColumnWidth);
...
intgetColumnHeaderWidth(TableColumn col, JTable table)
Figures out the width of the column header for the given TableColumn.
String methodName = MODULE_NAME + "getColumnHeaderWidth(TableColumn.JTable)";
int retval = -1;
TableCellRenderer renderer = col.getHeaderRenderer();
if (renderer != null) {
    Component comp = renderer.getTableCellRendererComponent(table, col.getHeaderValue(), false, false, 0,
            0);
    retval = comp.getPreferredSize().width;
} else 
...
DimensiongetHeaderDimension(JTable table, TableColumn tableColumn)
get Header Dimension
TableCellRenderer renderer = tableColumn.getHeaderRenderer();
if (renderer == null) {
    renderer = table.getTableHeader().getDefaultRenderer();
Component comp = renderer.getTableCellRendererComponent(table, tableColumn.getHeaderValue(), false, false,
        0, 0);
return comp.getPreferredSize();
intgetMaxColumnWidth(JTable aTable, int columnNo, boolean includeColumnHeaderWidth, int columnPadding)
get Max Column Width
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);
...
intgetSelectedColumn(JTableHeader tableHeader)
Get's the selected column header for the given JTableHeader .
Object selectedColumnValue = tableHeader.getClientProperty(SELECTED_COLUMN_KEY);
return selectedColumnValue != null && selectedColumnValue instanceof Integer
        ? ((Integer) selectedColumnValue)
        : NO_COLUMN;