Java JTable Column Width Set adjustColumnWidth(JTable tbl, int col, int maxColumnWidth)

Here you can find the source of adjustColumnWidth(JTable tbl, int col, int maxColumnWidth)

Description

(originally from @class org.openstreetmap.josm.gui.preferences.SourceEditor) adjust the preferred width of column col to the maximum preferred width of the cells requires JTable.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);

License

GNU General Public License

Declaration

public static void adjustColumnWidth(JTable tbl, int col, int maxColumnWidth) 

Method Source Code


//package com.java2s;
// License: GPL. For details, see LICENSE file.

import java.awt.Component;
import javax.swing.JTable;
import javax.swing.table.TableCellRenderer;

public class Main {
    /**/*w  w  w  .j a v a  2 s.co m*/
    * (originally from @class org.openstreetmap.josm.gui.preferences.SourceEditor)
    * adjust the preferred width of column col to the maximum preferred width of the cells
    * requires JTable.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
    */
    public static void adjustColumnWidth(JTable tbl, int col, int maxColumnWidth) {
        int maxwidth = 0;
        for (int row = 0; row < tbl.getRowCount(); row++) {
            TableCellRenderer tcr = tbl.getCellRenderer(row, col);
            Object val = tbl.getValueAt(row, col);
            Component comp = tcr.getTableCellRendererComponent(tbl, val, false, false, row, col);
            maxwidth = Math.max(comp.getPreferredSize().width, maxwidth);
        }
        tbl.getColumnModel().getColumn(col).setPreferredWidth(Math.min(maxwidth + 10, maxColumnWidth));
    }
}

Related

  1. adjustColumnPreferredWidths(JTable table)
  2. adjustColumnPreferredWidths(JTable table)
  3. adjustColumnWidth(JTable table, int buf)
  4. adjustColumnWidth(TableModel model, int columnIndex, int maxWidth, int rightPadding, JTable table)
  5. adjustColWidth(JTable table, int firstColumnWidth, int middleColumnWidth, int lastColumnWidth, int padding)
  6. autoFitColumnWidth(JTable table, TableColumn tableColumn)
  7. autoResizeColWidth(final JTable table, final DefaultTableModel model)