Example usage for javax.swing.table TableColumn setPreferredWidth

List of usage examples for javax.swing.table TableColumn setPreferredWidth

Introduction

In this page you can find the example usage for javax.swing.table TableColumn setPreferredWidth.

Prototype

@BeanProperty(description = "The preferred width of the column.")
public void setPreferredWidth(int preferredWidth) 

Source Link

Document

Sets this column's preferred width to preferredWidth.

Usage

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   www  .j  a 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: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 v a2s .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 a  v a 2s  .com*/
        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);//ww  w.  j a v  a2  s .  c o  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:com.hp.alm.ali.idea.ui.MultipleItemsDialog.java

private TableColumn createColumn(int index, TableModel model, int preferredWidth, Integer minWidth) {
    TableColumn column = new TableColumn(index);
    column.setPreferredWidth(preferredWidth);
    if (minWidth != null) {
        column.setMinWidth(minWidth);//from   w w  w.  j a  v  a 2s .co m
    }
    column.setHeaderValue(model.getColumnName(index));
    return column;
}

From source file:com.microsoft.intellij.ui.AppInsightsMngmtPanel.java

protected void init() {
    insightsTable.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
    insightsTable.setModel(new InsightsTableModel(getTableContent()));
    for (int i = 0; i < insightsTable.getColumnModel().getColumnCount(); i++) {
        TableColumn each = insightsTable.getColumnModel().getColumn(i);
        each.setPreferredWidth(InsightsTableModel.getColumnWidth(i, 450));
    }//from  www  .ja v a 2s.  c  o m
    importInstrumentationKeysFromButton.addActionListener(importButtonListener());
    newButton.addActionListener(newButtonListener());
    removeButton.addActionListener(removeButtonListener());
    addButton.addActionListener(addButtonListener());
    detailsButton.addActionListener(detailsButtonListener());

    detailsButton.setEnabled(false);
    removeButton.setEnabled(false);

    insightsTable.getSelectionModel().addListSelectionListener(createAccountsTableListener());
}

From source file:de.mprengemann.intellij.plugin.androidicons.dialogs.AndroidBatchScaleImporter.java

private void initColumnSizes() {
    table.addComponentListener(new ComponentAdapter() {
        @Override/*from   w w w.ja  v a2s  .  c  o  m*/
        public void componentResized(ComponentEvent componentEvent) {
            super.componentResized(componentEvent);
            Dimension tableSize = table.getSize();
            final int[] columnSizes = new int[] { 20, 20, 20, 40 };
            for (int i = 0; i < table.getColumnCount(); i++) {
                TableColumn column = table.getColumnModel().getColumn(i);
                column.setPreferredWidth((int) (tableSize.width * (columnSizes[i] / 100f)));
            }
        }
    });
}

From source file:org.zaproxy.zap.extension.multiFuzz.impl.http.HttpFuzzResultDialog.java

@Override
public JXTreeTable getTable() {
    if (table == null) {
        if (model == null) {
            model = new HttpFuzzTableModel();
        }/* ww w  .  ja v a 2 s  .  c o  m*/
        table = new JXTreeTable(model);
        table.setDoubleBuffered(true);
        table.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_INTERVAL_SELECTION);
        table.setName("HttpFuzzResultTable");
        table.setFont(new java.awt.Font("Default", java.awt.Font.PLAIN, 12));
        table.setDefaultRenderer(Pair.class, new IconTableCellRenderer());

        int[] widths = { 10, 25, 550, 30, 85, 55, 40, 70 };
        for (int i = 0, count = widths.length; i < count; i++) {
            TableColumn column = table.getColumnModel().getColumn(i);
            column.setPreferredWidth(widths[i]);
        }
        table.addMouseListener(new java.awt.event.MouseAdapter() {
            @Override
            public void mousePressed(java.awt.event.MouseEvent e) {
                showPopupMenuIfTriggered(e);
            }

            @Override
            public void mouseReleased(java.awt.event.MouseEvent e) {
                showPopupMenuIfTriggered(e);
            }

            private void showPopupMenuIfTriggered(java.awt.event.MouseEvent e) {
                if (e.isPopupTrigger()) {
                    if (e.isPopupTrigger()) {
                        // Select list item on right click
                        JTable table = (JTable) e.getSource();
                        int row = table.rowAtPoint(e.getPoint());

                        if (!table.isRowSelected(row)) {
                            table.changeSelection(row, 0, false, false);
                        }
                        View.getSingleton().getPopupMenu().show(e.getComponent(), e.getX(), e.getY());
                    }
                }
            }

        });
        table.getSelectionModel().addListSelectionListener(new javax.swing.event.ListSelectionListener() {

            @Override
            public void valueChanged(javax.swing.event.ListSelectionEvent e) {
                if (!e.getValueIsAdjusting()) {
                    if (table.getSelectedRowCount() == 0) {
                        return;
                    }
                    final int row = table.getSelectedRow();
                    if (getEntry(row) instanceof HttpFuzzRequestRecord) {
                        final HistoryReference historyReference = ((HttpFuzzRequestRecord) getEntry(row))
                                .getHistory();
                        try {
                            getMessageInspection().setMessage(historyReference.getHttpMessage());
                        } catch (HttpMalformedHeaderException | SQLException ex) {
                            logger.error(ex.getMessage(), ex);
                        }
                    }
                    updateValues();
                    redrawDiagrams();
                }
            }
        });
    }
    table.getTableHeader().addMouseListener(new MouseListener() {
        int sortedOn = -1;

        @Override
        public void mouseReleased(MouseEvent arg0) {
        }

        @Override
        public void mousePressed(MouseEvent arg0) {
        }

        @Override
        public void mouseExited(MouseEvent arg0) {
        }

        @Override
        public void mouseEntered(MouseEvent arg0) {
        }

        @Override
        public void mouseClicked(MouseEvent e) {
            int index = table.columnAtPoint(e.getPoint());
            List<HttpFuzzRecord> list = model.getEntries();
            if (list.size() == 0) {
                return;
            }
            HttpFuzzRecordComparator comp = new HttpFuzzRecordComparator();
            comp.setFeature(index);
            if (index == sortedOn) {
                Collections.sort(list, comp);
                Collections.reverse(list);
                sortedOn = -1;
            } else {
                Collections.sort(list, comp);
                sortedOn = index;
            }
            table.updateUI();
        }
    });
    table.setRootVisible(false);
    table.setVisible(true);
    return table;
}

From source file:org.nekorp.workflow.desktop.view.CostoServicioView.java

private void setDefaultColumnSize() {
    int[] sizeColumns = new int[] { 80, //tipo
            180, //concepto
            70, //cantidad
            70, //precio unitario
            70, //utilidad
            70, //precio cliente
            70//subtotal
    };/*from   w w w. jav  a 2  s . c  o  m*/
    TableColumn column;
    for (int i = 0; i < this.tablaCostos.getColumnCount() && i < sizeColumns.length; i++) {
        column = this.tablaCostos.getColumnModel().getColumn(i);
        column.setPreferredWidth(sizeColumns[i]);
    }
}

From source file:edu.ku.brc.af.ui.forms.formatters.DataObjFieldFormatMultiplePanel.java

/**
 * //from w w w . j  ava  2  s  .c o m
 */
private void setFormatSwitchTblColumnProperties() {
    // set details of 1st column (field values)
    TableColumnModel model = formatSwitchTbl.getColumnModel();
    TableColumn column = model.getColumn(model.getColumnIndex(FIELD_VALUE_COL));
    column.setMinWidth(20);
    column.setMaxWidth(300);
    column.setPreferredWidth(70);
    DefaultTableCellRenderer renderer = new DefaultTableCellRenderer();
    renderer.setHorizontalAlignment(SwingConstants.CENTER);
    column.setCellRenderer(renderer);

    // set details of 3rd column (ellipsis buttons)
    column = model.getColumn(model.getColumnIndex(ELLIPSIS_BUTTON_COL));
    column.setCellRenderer(new EditDataObjFormatButtonRenderer());
    column.setCellEditor(new EditDataObjFormatButtonEditor(createCheckBox()));
    column.setMinWidth(20);
    column.setMaxWidth(20);
    column.setPreferredWidth(20);
}