1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.gui.util;
|
---|
3 |
|
---|
4 | import java.awt.Component;
|
---|
5 |
|
---|
6 | import javax.swing.JTable;
|
---|
7 | import javax.swing.table.TableCellRenderer;
|
---|
8 | import javax.swing.table.TableColumn;
|
---|
9 |
|
---|
10 | /**
|
---|
11 | * The class that provide common JTable customization methods
|
---|
12 | * @since 5785
|
---|
13 | */
|
---|
14 | public 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) {
|
---|
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 |
|
---|
43 | /**
|
---|
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)
|
---|
62 | * requires JTable.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
|
---|
63 | * @param tbl table
|
---|
64 | * @param col column index
|
---|
65 | * @param maxColumnWidth maximum column width
|
---|
66 | */
|
---|
67 | public static void adjustColumnWidth(JTable tbl, int col, int maxColumnWidth) {
|
---|
68 | int maxwidth = getMaxWidth(tbl, col);
|
---|
69 | tbl.getColumnModel().getColumn(col).setPreferredWidth(Math.min(maxwidth+10, maxColumnWidth));
|
---|
70 | }
|
---|
71 |
|
---|
72 | /**
|
---|
73 | * adjust the table's columns to fit their content best
|
---|
74 | * requires JTable.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
|
---|
75 | * @param tbl table
|
---|
76 | * @since 14476
|
---|
77 | */
|
---|
78 | public static void computeColumnsWidth(JTable tbl) {
|
---|
79 | for (int column = 0; column < tbl.getColumnCount(); column++) {
|
---|
80 | adjustColumnWidth(tbl, column, Integer.MAX_VALUE);
|
---|
81 | }
|
---|
82 | }
|
---|
83 | }
|
---|