source: josm/trunk/src/org/openstreetmap/josm/gui/util/TableHelper.java@ 5927

Last change on this file since 5927 was 5927, checked in by bastiK, 11 years ago

add missing license information

File size: 1.1 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.gui.util;
3
4import java.awt.Component;
5
6import javax.swing.JTable;
7import javax.swing.table.TableCellRenderer;
8
9/**
10 * The class that provide common JTable customization methods
11 */
12public class TableHelper {
13 /**
14 * (originally from @class org.openstreetmap.josm.gui.preferences.SourceEditor)
15 * adjust the preferred width of column col to the maximum preferred width of the cells
16 * requires JTable.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
17 */
18 public static void adjustColumnWidth(JTable tbl, int col, int maxColumnWidth) {
19 int maxwidth = 0;
20 for (int row=0; row<tbl.getRowCount(); row++) {
21 TableCellRenderer tcr = tbl.getCellRenderer(row, col);
22 Object val = tbl.getValueAt(row, col);
23 Component comp = tcr.getTableCellRendererComponent(tbl, val, false, false, row, col);
24 maxwidth = Math.max(comp.getPreferredSize().width, maxwidth);
25 }
26 tbl.getColumnModel().getColumn(col).setPreferredWidth(Math.min(maxwidth+10, maxColumnWidth));
27 }
28
29}
Note: See TracBrowser for help on using the repository browser.