Example usage for javax.swing.table TableColumn getHeaderRenderer

List of usage examples for javax.swing.table TableColumn getHeaderRenderer

Introduction

In this page you can find the example usage for javax.swing.table TableColumn getHeaderRenderer.

Prototype

public TableCellRenderer getHeaderRenderer() 

Source Link

Document

Returns the TableCellRenderer used to draw the header of the TableColumn.

Usage

From source file:Main.java

/**
 *  Calculates the optimal width for the header of the given table. The
 *  calculation is based on the preferred width of the header renderer.
 *
 *  @param table    the table to calculate the column width
 *  @param col      the column to calculate the widths
 *  @return         the width, -1 if error
 *//*from   w w w  .  j  a  va2 s.  c o m*/
public static int calcHeaderWidth(JTable table, int col) {
    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;
    if (header != null)
        defaultHeaderRenderer = header.getDefaultRenderer();
    TableColumnModel columns = table.getColumnModel();
    TableModel data = table.getModel();
    TableColumn column = columns.getColumn(col);
    int width = -1;
    TableCellRenderer h = column.getHeaderRenderer();
    if (h == null)
        h = defaultHeaderRenderer;
    if (h != null) {
        // Not explicitly impossible
        Component c = h.getTableCellRendererComponent(table, column.getHeaderValue(), false, false, -1, col);
        width = c.getPreferredSize().width + 5;
    }

    return width;
}

From source file:Main.java

/**
 * Pack specified column. Sets the preferred width of the specified visible
 * column so it is will be just wide enough to show the column head and the
 * widest cell value in the column + {@code margin} each side.
 * //from  www.j  a v a2 s  .co  m
 * Taken from http://www.exampledepot.com/egs/javax.swing.table/PackCol.html
 * 
 * NB: If a table has more than 100 rows, then only the width of the column
 * header will be checked and not the widest cell value. Otherwise it could
 * take too long.
 * 
 * @param table
 *            table with column to pack
 * @param vColIndex
 *            column to pack
 * @param margin
 *            margin to leave at each side of the column (resulting in an
 *            additional width of 2*margin pixels).
 */
public static void packColumn(JTable table, int vColIndex, int margin) {

    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;

    // because checking the width of each row can be time consuming,
    // only do so if less than 101 rows.
    boolean checkDataWidth = (table.getRowCount() < 101);

    if (checkDataWidth) {
        // Get maximum width from all 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;

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

From source file:Main.java

/**
 * Resizes the columns to match content while keeping the table the same
 * size. This means that the last column may be larger than the content.
 *///from   w  w w . j a va  2s .  c  o m
public static void resizeTableColumns(JTable table) {
    table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);

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

        if (i == table.getColumnCount() - 1) {
            col.setPreferredWidth(table.getWidth() - totalWidth);
            table.setAutoResizeMode(JTable.AUTO_RESIZE_LAST_COLUMN);
            return;
        }

        int width = 0;
        TableCellRenderer renderer = col.getHeaderRenderer();
        if (renderer == null) {
            renderer = table.getTableHeader().getDefaultRenderer();
        }
        Component comp = renderer.getTableCellRendererComponent(table, col.getHeaderValue(), false, false, -1,
                i);
        width = Math.max(width, comp.getPreferredSize().width);
        for (int r = 0; r < table.getRowCount(); r++) {
            renderer = table.getCellRenderer(r, i);
            comp = renderer.getTableCellRendererComponent(table, table.getValueAt(r, i), false, false, r, i);
            width = Math.max(width, comp.getPreferredSize().width);
        }
        totalWidth += width + 2;
        col.setPreferredWidth(width + 2);
    }
}

From source file:Main.java

/**
 * Adjusts the widths and heights of the cells of the supplied table to fit their contents.
 *//*  w  w  w.  ja v a 2 s.  com*/
public static void sizeToContents(JTable table) {
    TableModel model = table.getModel();
    TableColumn column = null;
    Component comp = null;
    int ccount = table.getColumnModel().getColumnCount(), rcount = model.getRowCount(), cellHeight = 0;

    for (int cc = 0; cc < ccount; cc++) {
        int headerWidth = 0, cellWidth = 0;
        column = table.getColumnModel().getColumn(cc);
        try {
            comp = column.getHeaderRenderer().getTableCellRendererComponent(null, column.getHeaderValue(),
                    false, false, 0, 0);
            headerWidth = comp.getPreferredSize().width;
        } catch (NullPointerException e) {
            // getHeaderRenderer() this doesn't work in 1.3
        }

        for (int rr = 0; rr < rcount; rr++) {
            Object cellValue = model.getValueAt(rr, cc);
            comp = table.getDefaultRenderer(model.getColumnClass(cc)).getTableCellRendererComponent(table,
                    cellValue, false, false, 0, cc);
            Dimension psize = comp.getPreferredSize();
            cellWidth = Math.max(psize.width, cellWidth);
            cellHeight = Math.max(psize.height, cellHeight);
        }
        column.setPreferredWidth(Math.max(headerWidth, cellWidth));
    }

    if (cellHeight > 0) {
        table.setRowHeight(cellHeight);
    }
}

From source file:net.sf.webphotos.util.Util.java

/**
 * PackColumn sets the preferred width of the visible column specified by
 * vColIndex. The column will be just wide enough to show the column head
 * and the widest cell in the column. margin pixels are added to the left
 * and right (resulting in an additional width of 2*margin pixels).
 *
 * @param table The table you want to resize a column.
 * @param vColIndex The column number.//www .j a v  a  2  s. c o  m
 * @param margin Extra spaces for each side of column.
 */
public static void packColumn(JTable table, int vColIndex, int margin) {
    DefaultTableColumnModel colModel = (DefaultTableColumnModel) table.getColumnModel();
    TableColumn col = colModel.getColumn(vColIndex);
    int width;

    // Get width of column header
    javax.swing.table.TableCellRenderer renderer = col.getHeaderRenderer();
    if (renderer == null) {
        renderer = table.getTableHeader().getDefaultRenderer();
    }
    java.awt.Component comp = renderer.getTableCellRendererComponent(table, col.getHeaderValue(), false, false,
            0, 0);
    width = comp.getPreferredSize().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;

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

From source file:edu.ku.brc.stats.StatGroupTable.java

/**
 * Calculates and sets the each column to it preferred size
 * @param table the table to fix ups/* ww  w . ja  v  a2 s.c o  m*/
 */
public static void calcColumnWidths(JTable table) {
    JTableHeader header = table.getTableHeader();

    TableCellRenderer defaultHeaderRenderer = null;

    if (header != null) {
        defaultHeaderRenderer = header.getDefaultRenderer();
    }

    TableColumnModel columns = table.getColumnModel();
    TableModel data = table.getModel();

    int margin = columns.getColumnMargin(); // only JDK1.3

    int rowCount = data.getRowCount();

    int totalWidth = 0;

    for (int i = columns.getColumnCount() - 1; i >= 0; --i) {
        TableColumn column = columns.getColumn(i);

        int columnIndex = column.getModelIndex();

        int width = -1;

        TableCellRenderer h = column.getHeaderRenderer();

        if (h == null)
            h = defaultHeaderRenderer;

        if (h != null) // Not explicitly impossible
        {
            Component c = h.getTableCellRendererComponent(table, column.getHeaderValue(), false, false, -1, i);

            width = c.getPreferredSize().width;
        }

        for (int row = rowCount - 1; row >= 0; --row) {
            TableCellRenderer r = table.getCellRenderer(row, i);

            Component c = r.getTableCellRendererComponent(table, data.getValueAt(row, columnIndex), false,
                    false, row, i);

            width = Math.max(width, c.getPreferredSize().width + 10); // adding an arbitray 10 pixels to make it look nicer
        }

        if (width >= 0) {
            column.setPreferredWidth(width + margin); // <1.3: without margin
        }

        totalWidth += column.getPreferredWidth();
    }

    // If you like; This does not make sense for two many columns!
    Dimension size = table.getPreferredScrollableViewportSize();
    //if (totalWidth > size.width)
    {
        size.height = Math.min(size.height, table.getRowHeight() * visibleRows);
        size.width = totalWidth;
        table.setPreferredScrollableViewportSize(size);
    }

}

From source file:edu.ku.brc.specify.utilapps.SetUpBuildDlg.java

/**
 * @param table/* w w w  .  j  ava 2s  .c  om*/
 */
public static void calcColumnWidths(JTable table) {
    JTableHeader header = table.getTableHeader();

    TableCellRenderer defaultHeaderRenderer = null;

    if (header != null) {
        defaultHeaderRenderer = header.getDefaultRenderer();
    }

    TableColumnModel columns = table.getColumnModel();
    TableModel data = table.getModel();

    int margin = columns.getColumnMargin(); // only JDK1.3

    int rowCount = data.getRowCount();

    int totalWidth = 0;

    for (int i = columns.getColumnCount() - 1; i >= 0; --i) {
        TableColumn column = columns.getColumn(i);

        int columnIndex = column.getModelIndex();

        int width = -1;

        TableCellRenderer h = column.getHeaderRenderer();

        if (h == null)
            h = defaultHeaderRenderer;

        if (h != null) // Not explicitly impossible
        {
            Component c = h.getTableCellRendererComponent(table, column.getHeaderValue(), false, false, -1, i);

            width = c.getPreferredSize().width;
        }

        for (int row = rowCount - 1; row >= 0; --row) {
            TableCellRenderer r = table.getCellRenderer(row, i);

            Component c = r.getTableCellRendererComponent(table, data.getValueAt(row, columnIndex), false,
                    false, row, i);

            width = Math.max(width, c.getPreferredSize().width + 10); // adding an arbitray 10 pixels to make it look nicer
        }

        if (width >= 0) {
            column.setPreferredWidth(width + margin); // <1.3: without margin
        } else {
            // ???
        }

        totalWidth += column.getPreferredWidth();
    }
}

From source file:Main.java

public Main() {
    JTable table = new JTable(3, 3);
    table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);

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

        TableCellRenderer renderer = col.getHeaderRenderer();
        if (renderer == null) {
            renderer = table.getTableHeader().getDefaultRenderer();
        }/*from w w w  .j  av  a 2 s  .com*/
        Component comp = renderer.getTableCellRendererComponent(table, col.getHeaderValue(), false, false, 0,
                0);
        width = comp.getPreferredSize().width;
        col.setPreferredWidth(width + 2);
    }
    JFrame f = new JFrame();
    f.add(new JScrollPane(table));
    f.setSize(300, 300);
    f.setVisible(true);
}

From source file:Main.java

public Main() {
    JTable table = new JTable(3, 3);
    table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);

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

        TableCellRenderer renderer = col.getHeaderRenderer();
        for (int r = 0; r < table.getRowCount(); r++) {
            renderer = table.getCellRenderer(r, i);
            Component comp = renderer.getTableCellRendererComponent(table, table.getValueAt(r, i), false, false,
                    r, i);/* w w w .ja va 2 s. c  o m*/
            width = Math.max(width, comp.getPreferredSize().width);
        }
        col.setPreferredWidth(width + 2);
    }
    JFrame f = new JFrame();
    f.add(new JScrollPane(table));
    f.setSize(300, 300);
    f.setVisible(true);
}

From source file:GeMSE.GS.Analysis.Stats.OneSampleCovariancePanel.java

public void ResizeColumnWidth(JTable table) {
    final TableColumnModel columnModel = table.getColumnModel();
    for (int column = 0; column < table.getColumnCount(); column++) {
        int width = 50; // Min width

        TableColumn tableColumn = columnModel.getColumn(column);
        TableCellRenderer renderer = tableColumn.getHeaderRenderer();
        if (renderer == null)
            renderer = table.getTableHeader().getDefaultRenderer();

        Component component = renderer.getTableCellRendererComponent(table, tableColumn.getHeaderValue(), false,
                false, -1, column);//from   www  .jav  a  2s  .  co  m
        width = Math.max(component.getPreferredSize().width + 5, width);

        for (int row = 0; row < table.getRowCount(); row++) {
            renderer = table.getCellRenderer(row, column);
            Component comp = table.prepareRenderer(renderer, row, column);
            width = Math.max(comp.getPreferredSize().width + 5, width);
        }
        if (width > 400)
            width = 400;
        columnModel.getColumn(column).setPreferredWidth(width);
    }
}