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:Main.java

/**
 *
 * @param table/* w  ww  .  java2  s  .  c om*/
 * @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

/**
 * Packs all table rows to their preferred height.
 *
 * @param table table to process/*from w ww.  j  ava2s. c  om*/
 */
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

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 www  . j  a v  a 2  s  .co 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

/**
 *  Calculates the optimal width for the column of the given table. The
 *  calculation is based on the preferred width of the header and cell
 *  renderer.//w ww .  j  av a 2 s  .  c  o  m
 *  <br>
 *  Taken from the newsgoup de.comp.lang.java with some modifications.<br>
 *  Taken from FOPPS/EnhancedTable - http://fopps.sourceforge.net/<br>
 *
 *  @param table    the table to calculate the column width
 *  @param col      the column to calculate the widths
 *  @return         the width, -1 if error
 */
public static int calcColumnWidth(JTable table, int col) {
    int width = calcHeaderWidth(table, col);
    if (width == -1)
        return width;

    TableColumnModel columns = table.getColumnModel();
    TableModel data = table.getModel();
    int rowCount = data.getRowCount();
    TableColumn column = columns.getColumn(col);
    try {
        for (int row = rowCount - 1; row >= 0; --row) {
            Component c = table.prepareRenderer(table.getCellRenderer(row, col), row, col);
            width = Math.max(width, c.getPreferredSize().width + 10);
        }
    } catch (Exception e) {
        e.printStackTrace();
    }

    return width;
}

From source file:modnlp.capte.AlignmentInterfaceWS.java

public static int getPreferredRowHeight(JTable table, int rowIndex, int margin) {
    // Get the current default height for all rows
    int height = table.getRowHeight();

    // Determine highest cell in the row
    for (int c = 0; c < table.getColumnCount(); c++) {
        TableCellRenderer renderer = table.getCellRenderer(rowIndex, c);
        Component comp = table.prepareRenderer(renderer, rowIndex, c);
        int h = comp.getPreferredSize().height + 2 * margin;
        height = Math.max(height, h);
    }/*from ww  w . j  ava  2 s. c  o m*/
    return height;
}

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 w  w w.  j a  v a  2 s  . c om*/
 * 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: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 .  j  a  va 2s  .  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: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.
 *//*w  ww .  ja  va2 s .  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

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. j a  v a 2  s . com*/
            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:Visuals.RingChart.java

public JPanel addDefenceCharts() {
    lowValue = "Low (" + low + ")";
    ChartPanel ringPanel = drawRingChart();
    ringPanel.setDomainZoomable(true);//from  w  w  w . j a v  a2 s  .c  o  m
    JPanel thisRingPanel = new JPanel();
    thisRingPanel.setLayout(new BorderLayout());
    String[][] finalRisks = new String[riskCount][4];
    for (int i = 0; i < riskCount; i++) {
        finalRisks[i][0] = risks[i][0];
        finalRisks[i][1] = risks[i][1];
        finalRisks[i][2] = risks[i][2];
        finalRisks[i][3] = risks[i][3];
    }

    JTable table = new JTable(finalRisks, networkDefenceColumns);
    TableColumn tcol = table.getColumnModel().getColumn(2);
    table.removeColumn(tcol);
    table.setEnabled(false);

    table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);

    int width = 0;
    for (int row = 0; row < table.getRowCount(); row++) {
        TableCellRenderer renderer = table.getCellRenderer(row, 1);
        Component comp = table.prepareRenderer(renderer, row, 1);
        width = Math.max(comp.getPreferredSize().width, width);
    }

    table.getColumnModel().getColumn(1).setPreferredWidth(width);

    width = 0;
    for (int row = 0; row < table.getRowCount(); row++) {
        TableCellRenderer renderer = table.getCellRenderer(row, 2);
        Component comp = table.prepareRenderer(renderer, row, 2);
        width = Math.max(comp.getPreferredSize().width, width);
    }

    table.getColumnModel().getColumn(2).setPreferredWidth(width);
    table.setShowHorizontalLines(true);
    table.setRowHeight(40);

    JScrollPane tableScrollPane = new JScrollPane(table, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,
            JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);

    Dimension d = table.getPreferredSize();
    tableScrollPane
            .setPreferredSize(new Dimension((d.width - 400), (table.getRowHeight() + 1) * (riskCount + 1)));

    JLabel right = new JLabel(
            "                                                                                                    ");
    thisRingPanel.add(right, BorderLayout.EAST);
    thisRingPanel.add(ringPanel, BorderLayout.CENTER);
    thisRingPanel.add(tableScrollPane, BorderLayout.SOUTH);
    return thisRingPanel;
}