Example usage for javax.swing JTable getCellRenderer

List of usage examples for javax.swing JTable getCellRenderer

Introduction

In this page you can find the example usage for javax.swing JTable getCellRenderer.

Prototype

public TableCellRenderer getCellRenderer(int row, int column) 

Source Link

Document

Returns an appropriate renderer for the cell specified by this row and column.

Usage

From source file:net.sourceforge.squirrel_sql.fw.gui.action.wikiTable.GenericWikiTableTransformer.java

/**
 * @see//  ww  w .j a  va 2s . c o m
 * net.sourceforge.squirrel_sql.fw.gui.action.wikiTable.IWikiTableTransformer
 * #transform(javax.swing.JTable)
 */
@Override
public String transform(JTable table) {
    int nbrSelRows = table.getSelectedRowCount();
    int nbrSelCols = table.getSelectedColumnCount();
    int[] selRows = table.getSelectedRows();
    int[] selCols = table.getSelectedColumns();

    if (selRows.length != 0 && selCols.length != 0) {
        StringBuilder buf = new StringBuilder(1024);
        // Start the table
        appendWithReplacement(buf, configuration.getTableStartTag());
        // Create the header
        appendWithReplacement(buf, configuration.getHeaderStartTag());
        for (int colIdx = 0; colIdx < nbrSelCols; ++colIdx) {
            appendWithReplacement(buf, configuration.getHeaderCell(), table.getColumnName(selCols[colIdx]));
        }
        appendWithReplacement(buf, configuration.getHeaderEndTag());
        // Now fill all the table rows
        for (int rowIdx = 0; rowIdx < nbrSelRows; ++rowIdx) {
            appendWithReplacement(buf, configuration.getRowStartTag());
            for (int colIdx = 0; colIdx < nbrSelCols; ++colIdx) {
                TableCellRenderer cellRenderer = table.getCellRenderer(selRows[rowIdx], selCols[colIdx]);
                Object cellObj = table.getValueAt(selRows[rowIdx], selCols[colIdx]);

                if (cellRenderer instanceof SquirrelTableCellRenderer) {
                    cellObj = ((SquirrelTableCellRenderer) cellRenderer).renderValue(cellObj);
                }

                String value = null;
                if (cellObj == null) {
                    value = ""; //$NON-NLS-1$
                } else {
                    final String tmp = cellObj.toString();
                    if (tmp.trim().equals("")) { //$NON-NLS-1$
                        value = ""; //$NON-NLS-1$
                    } else {
                        value = tmp;
                    }
                }
                appendWithReplacement(buf, configuration.getDataCell(), value);
            }
            appendWithReplacement(buf, configuration.getRowEndTag());
        }
        appendWithReplacement(buf, configuration.getTableEndTag());
        return buf.toString();
    } else {
        return null;
    }

}

From source file:op.tools.SYSTools.java

public static void packColumn(JTable table, int vColIndex, int margin) {
    TableModel model = table.getModel();
    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();
    }/*w ww.  j  a va 2  s  .c  o m*/
    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);

    OPDE.debug("packColumn/3: col=" + vColIndex + "  width=" + width);
}

From source file:org.omegat.gui.align.AlignPanelController.java

private static void resizeRows(JTable table) {
    for (int row = 0; row < table.getRowCount(); row++) {
        int max = 0;
        for (int col = BeadTableModel.COL_SRC; col < table.getColumnCount(); col++) {
            int colWidth = table.getColumnModel().getColumn(col).getWidth();
            TableCellRenderer cellRenderer = table.getCellRenderer(row, col);
            Component c = table.prepareRenderer(cellRenderer, row, col);
            c.setBounds(0, 0, colWidth, Integer.MAX_VALUE);
            int height = c.getPreferredSize().height;
            max = Math.max(max, height);
        }//  w w  w  .ja  v a 2  s.com
        table.setRowHeight(row, max);
    }
}

From source file:org.svv.acmate.gui.ACTestingPanel.java

private void resizeColumnWidth(JTable table) {
    final TableColumnModel columnModel = table.getColumnModel();
    for (int column = 0; column < table.getColumnCount(); column++) {
        int width = 80; // Min width
        for (int row = 0; row < table.getRowCount(); row++) {
            TableCellRenderer renderer = table.getCellRenderer(row, column);
            Component comp = table.prepareRenderer(renderer, row, column);
            width = Math.max(comp.getPreferredSize().width + 1, width);
        }/*from   www  .ja va 2  s. co m*/
        columnModel.getColumn(column).setPreferredWidth(width);
    }
}

From source file:org.tellervo.desktop.io.ImportDialog.java

@SuppressWarnings("unused")
private static void updateRowHeights(JTable table) {
    try {/*www. j  av  a 2  s.c o  m*/
        for (int row = 0; row < table.getRowCount(); row++) {
            int rowHeight = table.getRowHeight();

            for (int column = 0; column < table.getColumnCount(); column++) {
                Component comp = table.prepareRenderer(table.getCellRenderer(row, column), row, column);
                rowHeight = Math.max(rowHeight, comp.getPreferredSize().height);
            }

            table.setRowHeight(row, rowHeight);
        }
    } catch (ClassCastException e) {
    }
}

From source file:org.yccheok.jstock.gui.JTableUtilities.java

public static void makeTableColumnWidthFit(JTable jTable, int col, int margin, boolean locking) {
    // strategy - get max width for cells in column and
    // make that the preferred width
    TableColumnModel columnModel = jTable.getColumnModel();
    int maxwidth = 0;

    for (int row = 0; row < jTable.getRowCount(); row++) {
        TableCellRenderer rend = jTable.getCellRenderer(row, col);
        Object value = jTable.getValueAt(row, col);
        Component comp = rend.getTableCellRendererComponent(jTable, value, false, false, row, col);

        maxwidth = Math.max(comp.getPreferredSize().width + margin, maxwidth);
    } // for row/*from  w  ww .j  a va 2  s.c  om*/

    TableColumn column = columnModel.getColumn(col);
    TableCellRenderer headerRenderer = column.getHeaderRenderer();
    if (headerRenderer == null) {
        headerRenderer = jTable.getTableHeader().getDefaultRenderer();
    }
    Object headerValue = column.getHeaderValue();
    Component headerComp = headerRenderer.getTableCellRendererComponent(jTable, headerValue, false, false, 0,
            col);

    maxwidth = Math.max(maxwidth, headerComp.getPreferredSize().width + margin);
    column.setPreferredWidth(maxwidth);
    if (locking) {
        // User will not able to adjust the width manually.
        column.setMinWidth(maxwidth);
        column.setMaxWidth(maxwidth);
    }
}