1 | package org.greenstone.gatherer.util;
|
---|
2 |
|
---|
3 | import java.awt.*;
|
---|
4 | import javax.swing.*;
|
---|
5 | import javax.swing.table.*;
|
---|
6 |
|
---|
7 | /**
|
---|
8 | * Utilites for manipulating JTables.
|
---|
9 | *
|
---|
10 | * @author Marc Hedlund
|
---|
11 | * <a href="mailto:[email protected]"><[email protected]></a>
|
---|
12 | * @version $Revision: 7540 $
|
---|
13 | */
|
---|
14 |
|
---|
15 | public class TableUtils {
|
---|
16 |
|
---|
17 | // This is based on code found at
|
---|
18 | // <http://java.sun.com/docs/books/tutorial/ui/swingComponents/table.html>,
|
---|
19 | // somewhat modified.
|
---|
20 |
|
---|
21 | // public static void setPreferredCellSizes(JTable table) {
|
---|
22 | // TableModel model = table.getModel();
|
---|
23 | // TableColumnModel colModel = table.getColumnModel();
|
---|
24 |
|
---|
25 | // for (int i = 0; i < model.getColumnCount(); i++) {
|
---|
26 | // TableColumn column = colModel.getColumn(i);
|
---|
27 |
|
---|
28 | // int longestCell = 0;
|
---|
29 | // int highestCell = 0;
|
---|
30 |
|
---|
31 | // for (int j = 0; j < model.getRowCount(); j++) {
|
---|
32 | // Object value = model.getValueAt(j, i);
|
---|
33 | // if (value == null) continue;
|
---|
34 |
|
---|
35 | // Component cell =
|
---|
36 | // table.getDefaultRenderer(model.getColumnClass(i)).
|
---|
37 | // getTableCellRendererComponent(table, value,
|
---|
38 | // false, false, j, i);
|
---|
39 |
|
---|
40 | // int width = cell.getPreferredSize().width;
|
---|
41 | // int height = cell.getPreferredSize().height;
|
---|
42 |
|
---|
43 | // if (width > longestCell) longestCell = width;
|
---|
44 | // if (height > highestCell) highestCell = height;
|
---|
45 | // }
|
---|
46 |
|
---|
47 | // Component headerComp = column.getHeaderRenderer().
|
---|
48 | // getTableCellRendererComponent(table, column.getHeaderValue(),
|
---|
49 | // false, false, 0, 0);
|
---|
50 |
|
---|
51 | // int headerWidth = headerComp.getPreferredSize().width;
|
---|
52 | // int headerHeight = headerComp.getPreferredSize().height;
|
---|
53 |
|
---|
54 | // column.setPreferredWidth(Math.max(headerWidth, longestCell));
|
---|
55 |
|
---|
56 | // int currentHeight = table.getRowHeight();
|
---|
57 | // int preferredHeight = Math.max(headerHeight, highestCell);
|
---|
58 |
|
---|
59 | // table.setRowHeight(Math.max(currentHeight, preferredHeight));
|
---|
60 | // }
|
---|
61 | // }
|
---|
62 |
|
---|
63 | /**
|
---|
64 | * Takes a column in an existing table and makes it fixed-width.
|
---|
65 | * Specifically, it sets the column's minimum and maximum widths to
|
---|
66 | * its preferred width, and disables auto-resize for the table as a
|
---|
67 | * whole.
|
---|
68 | *
|
---|
69 | * <p>
|
---|
70 | *
|
---|
71 | * Later on this should take a column array for efficiency.
|
---|
72 | *
|
---|
73 | * @param table JTable The table to modify
|
---|
74 | * @param colIndex int Which column to fix
|
---|
75 | * @return int The width of the column as it was fixed
|
---|
76 | */
|
---|
77 |
|
---|
78 | public static int fixColumnToPreferredWidth(JTable table, int colIndex) {
|
---|
79 | table.setAutoResizeMode(JTable.AUTO_RESIZE_NEXT_COLUMN);
|
---|
80 |
|
---|
81 | TableColumnModel tcm = table.getColumnModel();
|
---|
82 | TableColumn col = tcm.getColumn(colIndex);
|
---|
83 | int width = col.getPreferredWidth();
|
---|
84 |
|
---|
85 | col.setMaxWidth(width);
|
---|
86 | col.setMinWidth(width);
|
---|
87 |
|
---|
88 | //table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
|
---|
89 |
|
---|
90 | return width;
|
---|
91 | }
|
---|
92 | }
|
---|