1 package org.wcb.gui.util;
2
3 import javax.swing.*;
4 import javax.swing.table.TableColumnModel;
5 import javax.swing.table.TableColumn;
6 import javax.swing.table.TableCellRenderer;
7 import java.awt.*;
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35 public class TableUtils {
36
37
38
39
40
41
42
43
44 public static void setColumnWidths(JTable table, Insets insets,
45 boolean setMinimum,
46 boolean setMaximum) {
47 int columnCount = table.getColumnCount();
48 TableColumnModel tcm = table.getColumnModel();
49 int spare = (insets == null ? 0 : insets.left + insets.right);
50 for (int i = 0; i < columnCount; i++)
51 {
52 int width = calculateColumnWidth(table, i);
53 width += spare;
54 TableColumn column = tcm.getColumn(i);
55 column.setPreferredWidth(width);
56 if (setMinimum)
57 {
58 column.setMinWidth(width);
59 }
60 if (setMaximum)
61 {
62 column.setMaxWidth(width);
63 }
64 }
65
66 }
67
68
69 private static int calculateColumnWidth(JTable table,
70 int columnIndex) {
71 int width = 0;
72 int rowCount = table.getRowCount();
73 for (int i = 0; i < rowCount; i++)
74 {
75 TableCellRenderer renderer = table.getCellRenderer(i, columnIndex);
76 Component comp = renderer.getTableCellRendererComponent(
77 table, table.getValueAt(i, columnIndex),
78 false, false, i, columnIndex);
79 int thisWidth = comp.getPreferredSize().width;
80 if (thisWidth > width)
81 {
82 width = thisWidth;
83 }
84 }
85
86 return width;
87 }
88
89
90
91
92
93
94
95
96
97 public static void setColumnWidths(JTable table, FontMetrics metrics, Insets insets,
98 boolean setMinimum,
99 boolean setMaximum) {
100 int columnCount = table.getColumnCount();
101 TableColumnModel tcm = table.getColumnModel();
102 int spare = (insets == null ? 0 : insets.left + insets.right);
103 for (int i = 0; i < columnCount; i++)
104 {
105 int width = calculateColumnWidthMetrics(table, i, metrics);
106 width += spare;
107 TableColumn column = tcm.getColumn(i);
108 column.setPreferredWidth(width);
109 if (setMinimum)
110 {
111 column.setMinWidth(width);
112 }
113 if (setMaximum)
114 {
115 column.setMaxWidth(width);
116 }
117 }
118
119 }
120
121
122 private static int calculateColumnWidthMetrics(JTable table,
123 int columnIndex, FontMetrics metrics) {
124 int width = columnHeaderWidth(metrics, table, columnIndex);
125 int rowCount = table.getRowCount();
126 for (int i = 0; i < rowCount; i++)
127 {
128 TableCellRenderer renderer = table.getCellRenderer(i, columnIndex);
129 Component comp = renderer.getTableCellRendererComponent(
130 table, table.getValueAt(i, columnIndex),
131 false, false, i, columnIndex);
132 int thisWidth = comp.getPreferredSize().width;
133 if (thisWidth > width)
134 {
135 width = thisWidth;
136 }
137 }
138
139 return width;
140 }
141
142
143 private static int columnHeaderWidth(FontMetrics metrics, JTable table, int columnIndex) {
144 String value = table.getColumnName(columnIndex);
145
146 return metrics.stringWidth(value) + (2*table.getColumnModel().getColumnMargin());
147 }
148 }