Ignore:
Timestamp:
2019-06-16T23:25:10+02:00 (7 years ago)
Author:
Don-vip
Message:

fix #14208 - improve filter dialog table header (dynamic width, sortable)

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/org/openstreetmap/josm/gui/util/TableHelper.java

    r14476 r15176  
    66import javax.swing.JTable;
    77import javax.swing.table.TableCellRenderer;
     8import javax.swing.table.TableColumn;
    89
    910/**
    1011 * The class that provide common JTable customization methods
     12 * @since 5785
    1113 */
    1214public final class TableHelper {
     
    1618    }
    1719
     20    static int getColumnHeaderWidth(JTable tbl, int col) {
     21        TableColumn tableColumn = tbl.getColumnModel().getColumn(col);
     22        TableCellRenderer renderer = tableColumn.getHeaderRenderer();
     23
     24        if (renderer == null) {
     25            renderer = tbl.getTableHeader().getDefaultRenderer();
     26        }
     27
     28        Component c = renderer.getTableCellRendererComponent(tbl, tableColumn.getHeaderValue(), false, false, -1, col);
     29        return c.getPreferredSize().width;
     30    }
     31
     32    static int getMaxWidth(JTable tbl, int col) {
     33        int maxwidth = getColumnHeaderWidth(tbl, col);
     34        for (int row = 0; row < tbl.getRowCount(); row++) {
     35            TableCellRenderer tcr = tbl.getCellRenderer(row, col);
     36            Object val = tbl.getValueAt(row, col);
     37            Component comp = tcr.getTableCellRendererComponent(tbl, val, false, false, row, col);
     38            maxwidth = Math.max(comp.getPreferredSize().width, maxwidth);
     39        }
     40        return maxwidth;
     41    }
     42
    1843    /**
    19      * adjust the preferred width of column col to the maximum preferred width of the cells
     44     * adjust the preferred width of column col to the maximum preferred width of the cells (including header)
     45     * @param tbl table
     46     * @param col column index
     47     * @param resizable if true, resizing is allowed
     48     * @since 15176
     49     */
     50    public static void adjustColumnWidth(JTable tbl, int col, boolean resizable) {
     51        int maxwidth = getMaxWidth(tbl, col);
     52        TableColumn column = tbl.getColumnModel().getColumn(col);
     53        column.setPreferredWidth(maxwidth);
     54        column.setResizable(resizable);
     55        if (!resizable) {
     56            column.setMaxWidth(maxwidth);
     57        }
     58    }
     59
     60    /**
     61     * adjust the preferred width of column col to the maximum preferred width of the cells (including header)
    2062     * requires JTable.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
    2163     * @param tbl table
     
    2466     */
    2567    public static void adjustColumnWidth(JTable tbl, int col, int maxColumnWidth) {
    26         int maxwidth = 0;
    27         for (int row = 0; row < tbl.getRowCount(); row++) {
    28             TableCellRenderer tcr = tbl.getCellRenderer(row, col);
    29             Object val = tbl.getValueAt(row, col);
    30             Component comp = tcr.getTableCellRendererComponent(tbl, val, false, false, row, col);
    31             maxwidth = Math.max(comp.getPreferredSize().width, maxwidth);
    32         }
     68        int maxwidth = getMaxWidth(tbl, col);
    3369        tbl.getColumnModel().getColumn(col).setPreferredWidth(Math.min(maxwidth+10, maxColumnWidth));
    3470    }
Note: See TracChangeset for help on using the changeset viewer.