Changeset 15176 in josm for trunk/src/org/openstreetmap/josm/gui/util/TableHelper.java
- Timestamp:
- 2019-06-16T23:25:10+02:00 (7 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/org/openstreetmap/josm/gui/util/TableHelper.java
r14476 r15176 6 6 import javax.swing.JTable; 7 7 import javax.swing.table.TableCellRenderer; 8 import javax.swing.table.TableColumn; 8 9 9 10 /** 10 11 * The class that provide common JTable customization methods 12 * @since 5785 11 13 */ 12 14 public final class TableHelper { … … 16 18 } 17 19 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 18 43 /** 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) 20 62 * requires JTable.setAutoResizeMode(JTable.AUTO_RESIZE_OFF); 21 63 * @param tbl table … … 24 66 */ 25 67 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); 33 69 tbl.getColumnModel().getColumn(col).setPreferredWidth(Math.min(maxwidth+10, maxColumnWidth)); 34 70 }
Note:
See TracChangeset
for help on using the changeset viewer.
