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

Last change on this file since 15177 was 15177, checked in by Don-vip, 5 years ago

see #14208 - NPE

  • Property svn:eol-style set to native
File size: 3.0 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;
8import javax.swing.table.TableColumn;
9
10/**
11 * The class that provide common JTable customization methods
12 * @since 5785
13 */
14public final class TableHelper {
15
16 private TableHelper() {
17 // Hide default constructor for utils classes
18 }
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 && tbl.getTableHeader() != null)
25 renderer = tbl.getTableHeader().getDefaultRenderer();
26
27 if (renderer == null)
28 return 0;
29
30 Component c = renderer.getTableCellRendererComponent(tbl, tableColumn.getHeaderValue(), false, false, -1, col);
31 return c.getPreferredSize().width;
32 }
33
34 static int getMaxWidth(JTable tbl, int col) {
35 int maxwidth = getColumnHeaderWidth(tbl, col);
36 for (int row = 0; row < tbl.getRowCount(); row++) {
37 TableCellRenderer tcr = tbl.getCellRenderer(row, col);
38 Object val = tbl.getValueAt(row, col);
39 Component comp = tcr.getTableCellRendererComponent(tbl, val, false, false, row, col);
40 maxwidth = Math.max(comp.getPreferredSize().width, maxwidth);
41 }
42 return maxwidth;
43 }
44
45 /**
46 * adjust the preferred width of column col to the maximum preferred width of the cells (including header)
47 * @param tbl table
48 * @param col column index
49 * @param resizable if true, resizing is allowed
50 * @since 15176
51 */
52 public static void adjustColumnWidth(JTable tbl, int col, boolean resizable) {
53 int maxwidth = getMaxWidth(tbl, col);
54 TableColumn column = tbl.getColumnModel().getColumn(col);
55 column.setPreferredWidth(maxwidth);
56 column.setResizable(resizable);
57 if (!resizable) {
58 column.setMaxWidth(maxwidth);
59 }
60 }
61
62 /**
63 * adjust the preferred width of column col to the maximum preferred width of the cells (including header)
64 * requires JTable.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
65 * @param tbl table
66 * @param col column index
67 * @param maxColumnWidth maximum column width
68 */
69 public static void adjustColumnWidth(JTable tbl, int col, int maxColumnWidth) {
70 int maxwidth = getMaxWidth(tbl, col);
71 tbl.getColumnModel().getColumn(col).setPreferredWidth(Math.min(maxwidth+10, maxColumnWidth));
72 }
73
74 /**
75 * adjust the table's columns to fit their content best
76 * requires JTable.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
77 * @param tbl table
78 * @since 14476
79 */
80 public static void computeColumnsWidth(JTable tbl) {
81 for (int column = 0; column < tbl.getColumnCount(); column++) {
82 adjustColumnWidth(tbl, column, Integer.MAX_VALUE);
83 }
84 }
85}
Note: See TracBrowser for help on using the repository browser.