Example usage for javax.swing JTable prepareRenderer

List of usage examples for javax.swing JTable prepareRenderer

Introduction

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

Prototype

public Component prepareRenderer(TableCellRenderer renderer, int row, int column) 

Source Link

Document

Prepares the renderer by querying the data model for the value and selection state of the cell at row, column.

Usage

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./*from w  w w . j av a 2s  .  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 www  .  j  ava  2s .  c  o  m
    return height;
}

From source file:Visuals.RingChart.java

public JPanel addDefenceCharts() {
    lowValue = "Low (" + low + ")";
    ChartPanel ringPanel = drawRingChart();
    ringPanel.setDomainZoomable(true);//from w  ww .  j a v  a 2 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;
}

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 w w  w.j  a v  a2 s .c  om*/
        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);
    }
}

From source file:Visuals.RingChart.java

public JPanel addAVCharts() {
    JPanel thisPanel = new JPanel();
    thisPanel.setLayout(new BorderLayout());
    int tempCriticalCount = 0, tempUpdatedCount = 0;
    String[][] criticalList = new String[critical][4];
    String[][] updatedList = new String[low][4];
    for (int i = 0; i < riskCount; i++) {
        if (risks[i][2].equals("Critical")) {
            criticalList[tempCriticalCount][0] = risks[i][0];
            criticalList[tempCriticalCount][1] = risks[i][1];
            criticalList[tempCriticalCount][3] = risks[i][3];
            tempCriticalCount++;/*from w ww .jav a2s.  com*/
        }
        if (risks[i][2].equals("Low")) {
            updatedList[tempUpdatedCount][0] = risks[i][0];
            updatedList[tempUpdatedCount][1] = risks[i][1];
            tempUpdatedCount++;
        }
    }

    JTable criticalTable = new JTable(criticalList, criticalColumns);
    JTable updatedTable = new JTable(updatedList, updatedColumns);
    TableColumn tcol = criticalTable.getColumnModel().getColumn(2);
    criticalTable.removeColumn(tcol);
    TableColumn tcol2 = updatedTable.getColumnModel().getColumn(2);
    updatedTable.removeColumn(tcol2);

    criticalTable.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
    updatedTable.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);

    criticalTable.setEnabled(false);
    updatedTable.setEnabled(false);

    int width = 0;
    for (int i = 0; i < 3; i++) {
        for (int row = 0; row < criticalTable.getRowCount(); row++) {
            TableCellRenderer renderer = criticalTable.getCellRenderer(row, i);
            Component comp = criticalTable.prepareRenderer(renderer, row, i);
            width = Math.max(comp.getPreferredSize().width, width);
        }
        criticalTable.getColumnModel().getColumn(i).setPreferredWidth(width);
        width = 0;
    }

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

    criticalTable.setShowHorizontalLines(true);
    criticalTable.setRowHeight(40);
    updatedTable.setShowHorizontalLines(true);
    updatedTable.setRowHeight(40);

    JScrollPane criticalTableScrollPane = new JScrollPane(criticalTable,
            JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED, JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
    JScrollPane updatedTableScrollPane = new JScrollPane(updatedTable, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,
            JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);

    Dimension d = criticalTable.getPreferredSize();
    criticalTableScrollPane.setPreferredSize(
            new Dimension((d.width - 400), (criticalTable.getRowHeight() + 1) * (tempCriticalCount + 1)));

    Dimension d2 = updatedTable.getPreferredSize();
    updatedTableScrollPane.setPreferredSize(
            new Dimension((d.width - 400), (updatedTable.getRowHeight() + 1) * (tempUpdatedCount + 1)));

    Font titleFonts = new Font("Calibri", Font.BOLD, 30);

    JLabel criticalLabel = new JLabel(" Critical    ");
    criticalLabel.setFont(titleFonts);
    criticalLabel.setForeground(new Color(230, 27, 27));
    JPanel leftPanel = new JPanel();
    leftPanel.setLayout(new BorderLayout());
    leftPanel.add(criticalLabel, BorderLayout.WEST);
    leftPanel.add(criticalTableScrollPane, BorderLayout.CENTER);

    JLabel updatedLabel = new JLabel(" Updated ");
    updatedLabel.setFont(titleFonts);
    updatedLabel.setForeground(new Color(47, 196, 6));
    JPanel rightPanel = new JPanel();
    rightPanel.setLayout(new BorderLayout());
    rightPanel.add(updatedLabel, BorderLayout.WEST);
    rightPanel.add(updatedTableScrollPane, BorderLayout.CENTER);

    if (tempCriticalCount != 0)
        thisPanel.add(leftPanel, BorderLayout.NORTH);
    if (tempUpdatedCount != 0)
        thisPanel.add(rightPanel, BorderLayout.SOUTH);
    return thisPanel;
}

From source file:library.Form_Library.java

License:asdf

public static void resizeColumnWidth(JTable table) {
    final TableColumnModel columnModel = table.getColumnModel();
    for (int column = 0; column < table.getColumnCount(); column++) {
        int width = 15; // 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   w w w  .j  av a2s.c o m*/
        if (width > 300) {
            width = 300;
        }
        columnModel.getColumn(column).setPreferredWidth(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 .java 2s  . c  o  m*/
        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);
        }/* w w  w .ja  v a2  s.  c om*/
        columnModel.getColumn(column).setPreferredWidth(width);
    }
}

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

@SuppressWarnings("unused")
private static void updateRowHeights(JTable table) {
    try {//from  w  ww  .j ava  2  s  . co 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) {
    }
}