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

Last change on this file since 13661 was 9231, checked in by Don-vip, 8 years ago

javadoc update

  • Property svn:eol-style set to native
File size: 1.3 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 final class TableHelper {
13
14 private TableHelper() {
15 // Hide default constructor for utils classes
16 }
17
18 /**
19 * (originally from @class org.openstreetmap.josm.gui.preferences.SourceEditor)
20 * adjust the preferred width of column col to the maximum preferred width of the cells
21 * requires JTable.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
22 * @param tbl table
23 * @param col column index
24 * @param maxColumnWidth maximum column width
25 */
26 public static void adjustColumnWidth(JTable tbl, int col, int maxColumnWidth) {
27 int maxwidth = 0;
28 for (int row = 0; row < tbl.getRowCount(); row++) {
29 TableCellRenderer tcr = tbl.getCellRenderer(row, col);
30 Object val = tbl.getValueAt(row, col);
31 Component comp = tcr.getTableCellRendererComponent(tbl, val, false, false, row, col);
32 maxwidth = Math.max(comp.getPreferredSize().width, maxwidth);
33 }
34 tbl.getColumnModel().getColumn(col).setPreferredWidth(Math.min(maxwidth+10, maxColumnWidth));
35 }
36}
Note: See TracBrowser for help on using the repository browser.