Example usage for javax.swing.table DefaultTableColumnModel getColumn

List of usage examples for javax.swing.table DefaultTableColumnModel getColumn

Introduction

In this page you can find the example usage for javax.swing.table DefaultTableColumnModel getColumn.

Prototype

public TableColumn getColumn(int columnIndex) 

Source Link

Document

Returns the TableColumn object for the column at columnIndex.

Usage

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

/**
 * 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   ww w .j  a v a2s  .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.//from   w w  w  .  j a v a 2s .  co 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

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 .  c  om*/
        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);//from  w w w .j a  va 2s . co  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:edu.ku.brc.specify.plugins.sgr.SGRResultsDisplay.java

/**
 * From http://www.pikopong.com/blog/2008/08/13/auto-resize-jtable-column-width/
 * // www  . j  a v  a 2 s.c o  m
 * @param table
 * @param model
 * @return
 */
private JTable autoResizeColWidth(JTable table, DefaultTableModel model) {
    table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
    table.setModel(model);

    int margin = 5;

    for (int i = 0; i < table.getColumnCount(); i++) {
        int vColIndex = i;
        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;

        // 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);
    }

    ((DefaultTableCellRenderer) table.getTableHeader().getDefaultRenderer())
            .setHorizontalAlignment(SwingConstants.LEFT);

    // table.setAutoCreateRowSorter(true);
    //table.getTableHeader().setReorderingAllowed(false);

    return table;
}

From source file:modnlp.capte.AlignmentInterfaceWS.java

public JTable autoResizeColWidth(JTable table, DefaultTableModel model) {
    table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
    table.setModel(model);//w w w. j  av a 2s.c  o m

    int margin = 5;

    for (int i = 0; i < table.getColumnCount(); i++) {
        int vColIndex = i;
        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;

        // 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);
    }

    ((DefaultTableCellRenderer) table.getTableHeader().getDefaultRenderer())
            .setHorizontalAlignment(SwingConstants.LEFT);

    // table.setAutoCreateRowSorter(true);
    table.getTableHeader().setReorderingAllowed(false);

    return table;
}

From source file:edu.ku.brc.specify.tasks.subpane.ESResultsTablePanel.java

/**
 * @param table//www . ja v a2  s .  c  o  m
 * @param model
 */
protected void autoResizeColWidth(final JTable table, final DefaultTableModel model) {
    table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
    table.setModel(model);

    int margin = 5;

    DefaultTableColumnModel colModel = (DefaultTableColumnModel) table.getColumnModel();

    int preferredWidthTotal = 0;
    int renderedWidthTotal = 0;
    int[] colWidths = new int[table.getColumnCount()];
    int[] strWidths = new int[table.getColumnCount()];
    for (int i = 0; i < table.getColumnCount(); i++) {
        int vColIndex = i;
        TableColumn col = colModel.getColumn(vColIndex);
        int width = 0;

        TableCellRenderer headerRenderer = col.getHeaderRenderer();
        if (headerRenderer instanceof JLabel) {
            ((JLabel) headerRenderer).setHorizontalAlignment(SwingConstants.CENTER);
        }

        // Get width of column header
        TableCellRenderer renderer = col.getCellRenderer();
        if (renderer == null) {
            renderer = table.getTableHeader().getDefaultRenderer();
        }

        Component comp = renderer.getTableCellRendererComponent(table, col.getHeaderValue(), false, false, 0,
                0);

        width = comp.getPreferredSize().width;

        // Get maximum width of column data
        int strWidth = 0;
        boolean isString = model.getColumnClass(i) == String.class;
        for (int r = 0; r < table.getRowCount(); r++) {
            renderer = table.getCellRenderer(r, vColIndex);
            Object objVal = table.getValueAt(r, vColIndex);
            if (isString && objVal != null) {
                strWidth = Math.max(strWidth, ((String) objVal).length());
            }
            comp = renderer.getTableCellRendererComponent(table, objVal, false, false, r, vColIndex);
            width = Math.max(width, comp.getPreferredSize().width);
        }

        // Add margin
        width += 2 * margin;

        preferredWidthTotal += col.getPreferredWidth();
        colWidths[i] = width;
        strWidths[i] = strWidth;

        renderedWidthTotal += width;
    }

    if (renderedWidthTotal > preferredWidthTotal) {
        for (int i = 0; i < table.getColumnCount(); i++) {
            TableColumn col = colModel.getColumn(i);
            TableCellRenderer renderer = col.getCellRenderer();
            if (renderer != null) {
                ((JLabel) renderer).setHorizontalAlignment(
                        strWidths[i] > 20 ? SwingConstants.LEFT : SwingConstants.CENTER);
                //((JLabel)renderer).setHorizontalAlignment(SwingConstants.LEFT);
            }

            if (model.getColumnCount() > 3 && renderedWidthTotal > preferredWidthTotal) {
                col.setPreferredWidth(colWidths[i]);
            }
        }
    }

    ((DefaultTableCellRenderer) table.getTableHeader().getDefaultRenderer())
            .setHorizontalAlignment(SwingConstants.LEFT);

    // table.setAutoCreateRowSorter(true);
    table.getTableHeader().setReorderingAllowed(false);
}

From source file:edu.ku.brc.ui.UIHelper.java

/**
 * @param table//  w  w  w .  j  a  va2 s.c  o  m
 * @param model
 * @return
 */
public static JTable autoResizeColWidth(final JTable table, final DefaultTableModel model) {
    table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
    table.setModel(model);

    int margin = 5;

    DefaultTableColumnModel colModel = (DefaultTableColumnModel) table.getColumnModel();

    int preferredWidthTotal = 0;
    int renderedWidthTotal = 0;
    int[] colWidths = new int[table.getColumnCount()];
    for (int i = 0; i < table.getColumnCount(); i++) {
        int vColIndex = i;
        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;

        // 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;

        preferredWidthTotal += col.getPreferredWidth();
        colWidths[i] = width;

        renderedWidthTotal += width;
    }

    if (renderedWidthTotal > preferredWidthTotal) {
        for (int i = 0; i < table.getColumnCount(); i++) {
            colModel.getColumn(i).setPreferredWidth(colWidths[i]);
        }
    }

    ((DefaultTableCellRenderer) table.getTableHeader().getDefaultRenderer())
            .setHorizontalAlignment(SwingConstants.LEFT);

    // table.setAutoCreateRowSorter(true);
    table.getTableHeader().setReorderingAllowed(false);

    return table;
}

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();
    }//ww w. j  av  a2s .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);
}