Example usage for javax.swing.table TableCellRenderer getTableCellRendererComponent

List of usage examples for javax.swing.table TableCellRenderer getTableCellRendererComponent

Introduction

In this page you can find the example usage for javax.swing.table TableCellRenderer getTableCellRendererComponent.

Prototype

Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus,
        int row, int column);

Source Link

Document

Returns the component used for drawing the cell.

Usage

From source file:Main.java

public static void sizeWidthToFitData(JTable table, int vc, int buf) {
    TableColumn tc = table.getColumnModel().getColumn(vc);

    int max = table.getTableHeader().getDefaultRenderer()
            .getTableCellRendererComponent(table, tc.getHeaderValue(), false, false, 0, vc)
            .getPreferredSize().width;/*from   w  w  w .  ja va2 s.c  o  m*/

    int vrows = table.getRowCount();
    for (int i = 0; i < vrows; i++) {
        TableCellRenderer r = table.getCellRenderer(i, vc);
        Object value = table.getValueAt(i, vc);
        Component c = r.getTableCellRendererComponent(table, value, false, false, i, vc);
        int w = c.getPreferredSize().width;
        if (max < w) {
            max = w;
        }
    }

    tc.setPreferredWidth(max + buf);
}

From source file:Main.java

/**
 *
 * @param table/*from w w w .  jav  a2 s. c  o  m*/
 * @param row
 * @param column
 * @param p
 * @return
 */
public static boolean pointOutsidePrefSize(JTable table, int row, int column, Point p) {
    if ((table.convertColumnIndexToModel(column) != 0) || (row == -1)) {
        return true;
    }

    TableCellRenderer tcr = table.getCellRenderer(row, column);
    Object value = table.getValueAt(row, column);
    Component cell = tcr.getTableCellRendererComponent(table, value, false, false, row, column);
    Dimension itemSize = cell.getPreferredSize();
    Rectangle cellBounds = table.getCellRect(row, column, false);
    cellBounds.width = itemSize.width;
    cellBounds.height = itemSize.height;

    // See if coords are inside
    // ASSUME: mouse x,y will never be < cell's x,y
    assert ((p.x >= cellBounds.x) && (p.y >= cellBounds.y));

    if ((p.x > (cellBounds.x + cellBounds.width)) || (p.y > (cellBounds.y + cellBounds.height))) {
        return true;
    }

    return false;
}

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  . ja  v a  2 s  . c om*/
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

/**
 * Packs all table rows to their preferred height.
 *
 * @param table table to process//from   ww w .j av a  2 s  .c  o m
 */
public static void packRowHeights(final JTable table) {
    for (int row = 0; row < table.getRowCount(); row++) {
        int maxHeight = 0;
        for (int column = 0; column < table.getColumnCount(); column++) {
            final TableCellRenderer cellRenderer = table.getCellRenderer(row, column);
            final Object valueAt = table.getValueAt(row, column);
            final Component renderer = cellRenderer.getTableCellRendererComponent(table, valueAt, false, false,
                    row, column);
            final int heightPreferable = renderer != null ? renderer.getPreferredSize().height : 0;
            maxHeight = Math.max(heightPreferable, maxHeight);
        }
        table.setRowHeight(row, maxHeight);
    }
}

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.
 * /*  w  w  w  . ja  v a 2s. c  o 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

/**
 *  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  ww .ja  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

/**
 * Auto fit the column of a table.//w  ww .  j  a  va2  s.  c o  m
 * @param table the table for which to auto fit the columns.
 * @param columnIndex the index of the column to auto fit.
 * @param maxWidth the maximum width that a column can take (like Integer.MAX_WIDTH).
 */
public static void autoFitTableColumn(JTable table, int columnIndex, int maxWidth) {
    TableModel model = table.getModel();
    TableCellRenderer headerRenderer = table.getTableHeader().getDefaultRenderer();
    int rowCount = table.getRowCount();
    for (int i = columnIndex >= 0 ? columnIndex : model.getColumnCount() - 1; i >= 0; i--) {
        TableColumn column = table.getColumnModel().getColumn(i);
        int headerWidth = headerRenderer
                .getTableCellRendererComponent(table, column.getHeaderValue(), false, false, 0, 0)
                .getPreferredSize().width;
        int cellWidth = 0;
        for (int j = 0; j < rowCount; j++) {
            Component comp = table.getDefaultRenderer(model.getColumnClass(i))
                    .getTableCellRendererComponent(table, table.getValueAt(j, i), false, false, 0, i);
            int preferredWidth = comp.getPreferredSize().width;
            // Artificial space to look nicer.
            preferredWidth += 10;
            cellWidth = Math.max(cellWidth, preferredWidth);
        }
        // Artificial space for the sort icon.
        headerWidth += 20;
        column.setPreferredWidth(Math.min(Math.max(headerWidth, cellWidth) + table.getRowMargin(), maxWidth));
        if (columnIndex >= 0) {
            break;
        }
    }
}

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./*  w  w  w. jav 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/*from ww w  . j  a va  2  s .co 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 ww.j a  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
        } else {
            // ???
        }

        totalWidth += column.getPreferredWidth();
    }
}