author | malenkov |
Tue, 29 Oct 2013 17:01:06 +0400 | |
changeset 21278 | ef8a3a2a72f2 |
parent 20458 | f2423fb3fd19 |
child 22574 | 7f8ce0c8c20a |
permissions | -rw-r--r-- |
2 | 1 |
/* |
5506 | 2 |
* Copyright (c) 1997, 2008, Oracle and/or its affiliates. All rights reserved. |
2 | 3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
4 |
* |
|
5 |
* This code is free software; you can redistribute it and/or modify it |
|
6 |
* under the terms of the GNU General Public License version 2 only, as |
|
5506 | 7 |
* published by the Free Software Foundation. Oracle designates this |
2 | 8 |
* particular file as subject to the "Classpath" exception as provided |
5506 | 9 |
* by Oracle in the LICENSE file that accompanied this code. |
2 | 10 |
* |
11 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
|
12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
13 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
14 |
* version 2 for more details (a copy is included in the LICENSE file that |
|
15 |
* accompanied this code). |
|
16 |
* |
|
17 |
* You should have received a copy of the GNU General Public License version |
|
18 |
* 2 along with this work; if not, write to the Free Software Foundation, |
|
19 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
20 |
* |
|
5506 | 21 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
22 |
* or visit www.oracle.com if you need additional information or have any |
|
23 |
* questions. |
|
2 | 24 |
*/ |
25 |
||
26 |
package javax.swing.table; |
|
27 |
||
28 |
import javax.swing.*; |
|
29 |
import javax.swing.border.*; |
|
30 |
import javax.swing.event.SwingPropertyChangeSupport; |
|
31 |
import java.lang.Integer; |
|
32 |
import java.awt.Color; |
|
33 |
import java.awt.Component; |
|
34 |
import java.io.Serializable; |
|
35 |
import java.beans.PropertyChangeEvent; |
|
36 |
import java.beans.PropertyChangeListener; |
|
37 |
||
38 |
/** |
|
39 |
* A <code>TableColumn</code> represents all the attributes of a column in a |
|
21278 | 40 |
* <code>JTable</code>, such as width, resizability, minimum and maximum width. |
2 | 41 |
* In addition, the <code>TableColumn</code> provides slots for a renderer and |
42 |
* an editor that can be used to display and edit the values in this column. |
|
43 |
* <p> |
|
44 |
* It is also possible to specify renderers and editors on a per type basis |
|
45 |
* rather than a per column basis - see the |
|
46 |
* <code>setDefaultRenderer</code> method in the <code>JTable</code> class. |
|
47 |
* This default mechanism is only used when the renderer (or |
|
48 |
* editor) in the <code>TableColumn</code> is <code>null</code>. |
|
49 |
* <p> |
|
50 |
* The <code>TableColumn</code> stores the link between the columns in the |
|
51 |
* <code>JTable</code> and the columns in the <code>TableModel</code>. |
|
52 |
* The <code>modelIndex</code> is the column in the |
|
53 |
* <code>TableModel</code>, which will be queried for the data values for the |
|
54 |
* cells in this column. As the column moves around in the view this |
|
55 |
* <code>modelIndex</code> does not change. |
|
56 |
* <p> |
|
57 |
* <b>Note:</b> Some implementations may assume that all |
|
58 |
* <code>TableColumnModel</code>s are unique, therefore we would |
|
59 |
* recommend that the same <code>TableColumn</code> instance |
|
60 |
* not be added more than once to a <code>TableColumnModel</code>. |
|
61 |
* To show <code>TableColumn</code>s with the same column of |
|
62 |
* data from the model, create a new instance with the same |
|
63 |
* <code>modelIndex</code>. |
|
64 |
* <p> |
|
65 |
* <strong>Warning:</strong> |
|
66 |
* Serialized objects of this class will not be compatible with |
|
67 |
* future Swing releases. The current serialization support is |
|
68 |
* appropriate for short term storage or RMI between applications running |
|
69 |
* the same version of Swing. As of 1.4, support for long term storage |
|
20458 | 70 |
* of all JavaBeans™ |
2 | 71 |
* has been added to the <code>java.beans</code> package. |
72 |
* Please see {@link java.beans.XMLEncoder}. |
|
73 |
* |
|
74 |
* @author Alan Chung |
|
75 |
* @author Philip Milne |
|
76 |
* @see javax.swing.table.TableColumnModel |
|
77 |
* |
|
78 |
* @see javax.swing.table.DefaultTableColumnModel |
|
79 |
* @see javax.swing.table.JTableHeader#getDefaultRenderer() |
|
80 |
* @see JTable#getDefaultRenderer(Class) |
|
81 |
* @see JTable#getDefaultEditor(Class) |
|
82 |
* @see JTable#getCellRenderer(int, int) |
|
83 |
* @see JTable#getCellEditor(int, int) |
|
84 |
*/ |
|
85 |
public class TableColumn extends Object implements Serializable { |
|
86 |
||
87 |
/** |
|
88 |
* Obsolete as of Java 2 platform v1.3. Please use string literals to identify |
|
89 |
* properties. |
|
90 |
*/ |
|
91 |
/* |
|
92 |
* Warning: The value of this constant, "columWidth" is wrong as the |
|
93 |
* name of the property is "columnWidth". |
|
94 |
*/ |
|
95 |
public final static String COLUMN_WIDTH_PROPERTY = "columWidth"; |
|
96 |
||
97 |
/** |
|
98 |
* Obsolete as of Java 2 platform v1.3. Please use string literals to identify |
|
99 |
* properties. |
|
100 |
*/ |
|
101 |
public final static String HEADER_VALUE_PROPERTY = "headerValue"; |
|
102 |
||
103 |
/** |
|
104 |
* Obsolete as of Java 2 platform v1.3. Please use string literals to identify |
|
105 |
* properties. |
|
106 |
*/ |
|
107 |
public final static String HEADER_RENDERER_PROPERTY = "headerRenderer"; |
|
108 |
||
109 |
/** |
|
110 |
* Obsolete as of Java 2 platform v1.3. Please use string literals to identify |
|
111 |
* properties. |
|
112 |
*/ |
|
113 |
public final static String CELL_RENDERER_PROPERTY = "cellRenderer"; |
|
114 |
||
115 |
// |
|
116 |
// Instance Variables |
|
117 |
// |
|
118 |
||
119 |
/** |
|
120 |
* The index of the column in the model which is to be displayed by |
|
121 |
* this <code>TableColumn</code>. As columns are moved around in the |
|
122 |
* view <code>modelIndex</code> remains constant. |
|
123 |
*/ |
|
124 |
protected int modelIndex; |
|
125 |
||
126 |
/** |
|
127 |
* This object is not used internally by the drawing machinery of |
|
128 |
* the <code>JTable</code>; identifiers may be set in the |
|
129 |
* <code>TableColumn</code> as as an |
|
130 |
* optional way to tag and locate table columns. The table package does |
|
131 |
* not modify or invoke any methods in these identifier objects other |
|
132 |
* than the <code>equals</code> method which is used in the |
|
133 |
* <code>getColumnIndex()</code> method in the |
|
134 |
* <code>DefaultTableColumnModel</code>. |
|
135 |
*/ |
|
136 |
protected Object identifier; |
|
137 |
||
138 |
/** The width of the column. */ |
|
139 |
protected int width; |
|
140 |
||
141 |
/** The minimum width of the column. */ |
|
142 |
protected int minWidth; |
|
143 |
||
144 |
/** The preferred width of the column. */ |
|
145 |
private int preferredWidth; |
|
146 |
||
147 |
/** The maximum width of the column. */ |
|
148 |
protected int maxWidth; |
|
149 |
||
150 |
/** The renderer used to draw the header of the column. */ |
|
151 |
protected TableCellRenderer headerRenderer; |
|
152 |
||
153 |
/** The header value of the column. */ |
|
154 |
protected Object headerValue; |
|
155 |
||
156 |
/** The renderer used to draw the data cells of the column. */ |
|
157 |
protected TableCellRenderer cellRenderer; |
|
158 |
||
159 |
/** The editor used to edit the data cells of the column. */ |
|
160 |
protected TableCellEditor cellEditor; |
|
161 |
||
162 |
/** If true, the user is allowed to resize the column; the default is true. */ |
|
163 |
protected boolean isResizable; |
|
164 |
||
165 |
/** |
|
166 |
* This field was not used in previous releases and there are |
|
167 |
* currently no plans to support it in the future. |
|
168 |
* |
|
169 |
* @deprecated as of Java 2 platform v1.3 |
|
170 |
*/ |
|
171 |
/* |
|
172 |
* Counter used to disable posting of resizing notifications until the |
|
173 |
* end of the resize. |
|
174 |
*/ |
|
175 |
@Deprecated |
|
176 |
transient protected int resizedPostingDisableCount; |
|
177 |
||
178 |
/** |
|
179 |
* If any <code>PropertyChangeListeners</code> have been registered, the |
|
180 |
* <code>changeSupport</code> field describes them. |
|
181 |
*/ |
|
182 |
private SwingPropertyChangeSupport changeSupport; |
|
183 |
||
184 |
// |
|
185 |
// Constructors |
|
186 |
// |
|
187 |
||
188 |
/** |
|
189 |
* Cover method, using a default model index of 0, |
|
190 |
* default width of 75, a <code>null</code> renderer and a |
|
191 |
* <code>null</code> editor. |
|
192 |
* This method is intended for serialization. |
|
193 |
* @see #TableColumn(int, int, TableCellRenderer, TableCellEditor) |
|
194 |
*/ |
|
195 |
public TableColumn() { |
|
196 |
this(0); |
|
197 |
} |
|
198 |
||
199 |
/** |
|
200 |
* Cover method, using a default width of 75, a <code>null</code> |
|
201 |
* renderer and a <code>null</code> editor. |
|
202 |
* @see #TableColumn(int, int, TableCellRenderer, TableCellEditor) |
|
203 |
*/ |
|
204 |
public TableColumn(int modelIndex) { |
|
205 |
this(modelIndex, 75, null, null); |
|
206 |
} |
|
207 |
||
208 |
/** |
|
209 |
* Cover method, using a <code>null</code> renderer and a |
|
210 |
* <code>null</code> editor. |
|
211 |
* @see #TableColumn(int, int, TableCellRenderer, TableCellEditor) |
|
212 |
*/ |
|
213 |
public TableColumn(int modelIndex, int width) { |
|
214 |
this(modelIndex, width, null, null); |
|
215 |
} |
|
216 |
||
217 |
/** |
|
218 |
* Creates and initializes an instance of |
|
219 |
* <code>TableColumn</code> with the specified model index, |
|
220 |
* width, cell renderer, and cell editor; |
|
221 |
* all <code>TableColumn</code> constructors delegate to this one. |
|
222 |
* The value of <code>width</code> is used |
|
223 |
* for both the initial and preferred width; |
|
224 |
* if <code>width</code> is negative, |
|
225 |
* they're set to 0. |
|
226 |
* The minimum width is set to 15 unless the initial width is less, |
|
227 |
* in which case the minimum width is set to |
|
228 |
* the initial width. |
|
229 |
* |
|
230 |
* <p> |
|
231 |
* When the <code>cellRenderer</code> |
|
232 |
* or <code>cellEditor</code> parameter is <code>null</code>, |
|
233 |
* a default value provided by the <code>JTable</code> |
|
234 |
* <code>getDefaultRenderer</code> |
|
235 |
* or <code>getDefaultEditor</code> method, respectively, |
|
236 |
* is used to |
|
237 |
* provide defaults based on the type of the data in this column. |
|
238 |
* This column-centric rendering strategy can be circumvented by overriding |
|
239 |
* the <code>getCellRenderer</code> methods in <code>JTable</code>. |
|
240 |
* |
|
241 |
* @param modelIndex the index of the column |
|
242 |
* in the model that supplies the data for this column in the table; |
|
243 |
* the model index remains the same |
|
244 |
* even when columns are reordered in the view |
|
245 |
* @param width this column's preferred width and initial width |
|
246 |
* @param cellRenderer the object used to render values in this column |
|
247 |
* @param cellEditor the object used to edit values in this column |
|
248 |
* @see #getMinWidth() |
|
249 |
* @see JTable#getDefaultRenderer(Class) |
|
250 |
* @see JTable#getDefaultEditor(Class) |
|
251 |
* @see JTable#getCellRenderer(int, int) |
|
252 |
* @see JTable#getCellEditor(int, int) |
|
253 |
*/ |
|
254 |
public TableColumn(int modelIndex, int width, |
|
255 |
TableCellRenderer cellRenderer, |
|
256 |
TableCellEditor cellEditor) { |
|
257 |
super(); |
|
258 |
this.modelIndex = modelIndex; |
|
259 |
preferredWidth = this.width = Math.max(width, 0); |
|
260 |
||
261 |
this.cellRenderer = cellRenderer; |
|
262 |
this.cellEditor = cellEditor; |
|
263 |
||
264 |
// Set other instance variables to default values. |
|
265 |
minWidth = Math.min(15, this.width); |
|
266 |
maxWidth = Integer.MAX_VALUE; |
|
267 |
isResizable = true; |
|
268 |
resizedPostingDisableCount = 0; |
|
269 |
headerValue = null; |
|
270 |
} |
|
271 |
||
272 |
// |
|
273 |
// Modifying and Querying attributes |
|
274 |
// |
|
275 |
||
276 |
private void firePropertyChange(String propertyName, Object oldValue, Object newValue) { |
|
277 |
if (changeSupport != null) { |
|
278 |
changeSupport.firePropertyChange(propertyName, oldValue, newValue); |
|
279 |
} |
|
280 |
} |
|
281 |
||
282 |
private void firePropertyChange(String propertyName, int oldValue, int newValue) { |
|
283 |
if (oldValue != newValue) { |
|
438
2ae294e4518c
6613529: Avoid duplicate object creation within JDK packages
dav
parents:
2
diff
changeset
|
284 |
firePropertyChange(propertyName, Integer.valueOf(oldValue), Integer.valueOf(newValue)); |
2 | 285 |
} |
286 |
} |
|
287 |
||
288 |
private void firePropertyChange(String propertyName, boolean oldValue, boolean newValue) { |
|
289 |
if (oldValue != newValue) { |
|
290 |
firePropertyChange(propertyName, Boolean.valueOf(oldValue), Boolean.valueOf(newValue)); |
|
291 |
} |
|
292 |
} |
|
293 |
||
294 |
/** |
|
295 |
* Sets the model index for this column. The model index is the |
|
296 |
* index of the column in the model that will be displayed by this |
|
297 |
* <code>TableColumn</code>. As the <code>TableColumn</code> |
|
298 |
* is moved around in the view the model index remains constant. |
|
299 |
* @param modelIndex the new modelIndex |
|
300 |
* @beaninfo |
|
301 |
* bound: true |
|
302 |
* description: The model index. |
|
303 |
*/ |
|
304 |
public void setModelIndex(int modelIndex) { |
|
305 |
int old = this.modelIndex; |
|
306 |
this.modelIndex = modelIndex; |
|
307 |
firePropertyChange("modelIndex", old, modelIndex); |
|
308 |
} |
|
309 |
||
310 |
/** |
|
311 |
* Returns the model index for this column. |
|
312 |
* @return the <code>modelIndex</code> property |
|
313 |
*/ |
|
314 |
public int getModelIndex() { |
|
315 |
return modelIndex; |
|
316 |
} |
|
317 |
||
318 |
/** |
|
319 |
* Sets the <code>TableColumn</code>'s identifier to |
|
320 |
* <code>anIdentifier</code>. <p> |
|
321 |
* Note: identifiers are not used by the <code>JTable</code>, |
|
322 |
* they are purely a |
|
323 |
* convenience for the external tagging and location of columns. |
|
324 |
* |
|
325 |
* @param identifier an identifier for this column |
|
326 |
* @see #getIdentifier |
|
327 |
* @beaninfo |
|
328 |
* bound: true |
|
329 |
* description: A unique identifier for this column. |
|
330 |
*/ |
|
331 |
public void setIdentifier(Object identifier) { |
|
332 |
Object old = this.identifier; |
|
333 |
this.identifier = identifier; |
|
334 |
firePropertyChange("identifier", old, identifier); |
|
335 |
} |
|
336 |
||
337 |
||
338 |
/** |
|
339 |
* Returns the <code>identifier</code> object for this column. |
|
340 |
* Note identifiers are not used by <code>JTable</code>, |
|
341 |
* they are purely a convenience for external use. |
|
342 |
* If the <code>identifier</code> is <code>null</code>, |
|
343 |
* <code>getIdentifier()</code> returns <code>getHeaderValue</code> |
|
344 |
* as a default. |
|
345 |
* |
|
346 |
* @return the <code>identifier</code> property |
|
347 |
* @see #setIdentifier |
|
348 |
*/ |
|
349 |
public Object getIdentifier() { |
|
350 |
return (identifier != null) ? identifier : getHeaderValue(); |
|
351 |
||
352 |
} |
|
353 |
||
354 |
/** |
|
355 |
* Sets the <code>Object</code> whose string representation will be |
|
356 |
* used as the value for the <code>headerRenderer</code>. When the |
|
357 |
* <code>TableColumn</code> is created, the default <code>headerValue</code> |
|
358 |
* is <code>null</code>. |
|
359 |
* @param headerValue the new headerValue |
|
360 |
* @see #getHeaderValue |
|
361 |
* @beaninfo |
|
362 |
* bound: true |
|
363 |
* description: The text to be used by the header renderer. |
|
364 |
*/ |
|
365 |
public void setHeaderValue(Object headerValue) { |
|
366 |
Object old = this.headerValue; |
|
367 |
this.headerValue = headerValue; |
|
368 |
firePropertyChange("headerValue", old, headerValue); |
|
369 |
} |
|
370 |
||
371 |
/** |
|
372 |
* Returns the <code>Object</code> used as the value for the header |
|
373 |
* renderer. |
|
374 |
* |
|
375 |
* @return the <code>headerValue</code> property |
|
376 |
* @see #setHeaderValue |
|
377 |
*/ |
|
378 |
public Object getHeaderValue() { |
|
379 |
return headerValue; |
|
380 |
} |
|
381 |
||
382 |
// |
|
383 |
// Renderers and Editors |
|
384 |
// |
|
385 |
||
386 |
/** |
|
387 |
* Sets the <code>TableCellRenderer</code> used to draw the |
|
388 |
* <code>TableColumn</code>'s header to <code>headerRenderer</code>. |
|
389 |
* <p> |
|
390 |
* It is the header renderers responsibility to render the sorting |
|
391 |
* indicator. If you are using sorting and specify a renderer your |
|
392 |
* renderer must render the sorting indication. |
|
393 |
* |
|
394 |
* @param headerRenderer the new headerRenderer |
|
395 |
* |
|
396 |
* @see #getHeaderRenderer |
|
397 |
* @beaninfo |
|
398 |
* bound: true |
|
399 |
* description: The header renderer. |
|
400 |
*/ |
|
401 |
public void setHeaderRenderer(TableCellRenderer headerRenderer) { |
|
402 |
TableCellRenderer old = this.headerRenderer; |
|
403 |
this.headerRenderer = headerRenderer; |
|
404 |
firePropertyChange("headerRenderer", old, headerRenderer); |
|
405 |
} |
|
406 |
||
407 |
/** |
|
408 |
* Returns the <code>TableCellRenderer</code> used to draw the header of the |
|
409 |
* <code>TableColumn</code>. When the <code>headerRenderer</code> is |
|
410 |
* <code>null</code>, the <code>JTableHeader</code> |
|
411 |
* uses its <code>defaultRenderer</code>. The default value for a |
|
412 |
* <code>headerRenderer</code> is <code>null</code>. |
|
413 |
* |
|
414 |
* @return the <code>headerRenderer</code> property |
|
415 |
* @see #setHeaderRenderer |
|
416 |
* @see #setHeaderValue |
|
417 |
* @see javax.swing.table.JTableHeader#getDefaultRenderer() |
|
418 |
*/ |
|
419 |
public TableCellRenderer getHeaderRenderer() { |
|
420 |
return headerRenderer; |
|
421 |
} |
|
422 |
||
423 |
/** |
|
424 |
* Sets the <code>TableCellRenderer</code> used by <code>JTable</code> |
|
425 |
* to draw individual values for this column. |
|
426 |
* |
|
427 |
* @param cellRenderer the new cellRenderer |
|
428 |
* @see #getCellRenderer |
|
429 |
* @beaninfo |
|
430 |
* bound: true |
|
431 |
* description: The renderer to use for cell values. |
|
432 |
*/ |
|
433 |
public void setCellRenderer(TableCellRenderer cellRenderer) { |
|
434 |
TableCellRenderer old = this.cellRenderer; |
|
435 |
this.cellRenderer = cellRenderer; |
|
436 |
firePropertyChange("cellRenderer", old, cellRenderer); |
|
437 |
} |
|
438 |
||
439 |
/** |
|
440 |
* Returns the <code>TableCellRenderer</code> used by the |
|
441 |
* <code>JTable</code> to draw |
|
442 |
* values for this column. The <code>cellRenderer</code> of the column |
|
443 |
* not only controls the visual look for the column, but is also used to |
|
444 |
* interpret the value object supplied by the <code>TableModel</code>. |
|
445 |
* When the <code>cellRenderer</code> is <code>null</code>, |
|
446 |
* the <code>JTable</code> uses a default renderer based on the |
|
447 |
* class of the cells in that column. The default value for a |
|
448 |
* <code>cellRenderer</code> is <code>null</code>. |
|
449 |
* |
|
450 |
* @return the <code>cellRenderer</code> property |
|
451 |
* @see #setCellRenderer |
|
452 |
* @see JTable#setDefaultRenderer |
|
453 |
*/ |
|
454 |
public TableCellRenderer getCellRenderer() { |
|
455 |
return cellRenderer; |
|
456 |
} |
|
457 |
||
458 |
/** |
|
459 |
* Sets the editor to used by when a cell in this column is edited. |
|
460 |
* |
|
461 |
* @param cellEditor the new cellEditor |
|
462 |
* @see #getCellEditor |
|
463 |
* @beaninfo |
|
464 |
* bound: true |
|
465 |
* description: The editor to use for cell values. |
|
466 |
*/ |
|
467 |
public void setCellEditor(TableCellEditor cellEditor){ |
|
468 |
TableCellEditor old = this.cellEditor; |
|
469 |
this.cellEditor = cellEditor; |
|
470 |
firePropertyChange("cellEditor", old, cellEditor); |
|
471 |
} |
|
472 |
||
473 |
/** |
|
474 |
* Returns the <code>TableCellEditor</code> used by the |
|
475 |
* <code>JTable</code> to edit values for this column. When the |
|
476 |
* <code>cellEditor</code> is <code>null</code>, the <code>JTable</code> |
|
477 |
* uses a default editor based on the |
|
478 |
* class of the cells in that column. The default value for a |
|
479 |
* <code>cellEditor</code> is <code>null</code>. |
|
480 |
* |
|
481 |
* @return the <code>cellEditor</code> property |
|
482 |
* @see #setCellEditor |
|
483 |
* @see JTable#setDefaultEditor |
|
484 |
*/ |
|
485 |
public TableCellEditor getCellEditor() { |
|
486 |
return cellEditor; |
|
487 |
} |
|
488 |
||
489 |
/** |
|
490 |
* This method should not be used to set the widths of columns in the |
|
491 |
* <code>JTable</code>, use <code>setPreferredWidth</code> instead. |
|
492 |
* Like a layout manager in the |
|
493 |
* AWT, the <code>JTable</code> adjusts a column's width automatically |
|
494 |
* whenever the |
|
495 |
* table itself changes size, or a column's preferred width is changed. |
|
496 |
* Setting widths programmatically therefore has no long term effect. |
|
497 |
* <p> |
|
498 |
* This method sets this column's width to <code>width</code>. |
|
499 |
* If <code>width</code> exceeds the minimum or maximum width, |
|
500 |
* it is adjusted to the appropriate limiting value. |
|
501 |
* @param width the new width |
|
502 |
* @see #getWidth |
|
503 |
* @see #setMinWidth |
|
504 |
* @see #setMaxWidth |
|
505 |
* @see #setPreferredWidth |
|
506 |
* @see JTable#doLayout() |
|
507 |
* @beaninfo |
|
508 |
* bound: true |
|
509 |
* description: The width of the column. |
|
510 |
*/ |
|
511 |
public void setWidth(int width) { |
|
512 |
int old = this.width; |
|
513 |
this.width = Math.min(Math.max(width, minWidth), maxWidth); |
|
514 |
firePropertyChange("width", old, this.width); |
|
515 |
} |
|
516 |
||
517 |
/** |
|
518 |
* Returns the width of the <code>TableColumn</code>. The default width is |
|
519 |
* 75. |
|
520 |
* |
|
521 |
* @return the <code>width</code> property |
|
522 |
* @see #setWidth |
|
523 |
*/ |
|
524 |
public int getWidth() { |
|
525 |
return width; |
|
526 |
} |
|
527 |
||
528 |
/** |
|
529 |
* Sets this column's preferred width to <code>preferredWidth</code>. |
|
530 |
* If <code>preferredWidth</code> exceeds the minimum or maximum width, |
|
531 |
* it is adjusted to the appropriate limiting value. |
|
532 |
* <p> |
|
533 |
* For details on how the widths of columns in the <code>JTable</code> |
|
534 |
* (and <code>JTableHeader</code>) are calculated from the |
|
535 |
* <code>preferredWidth</code>, |
|
536 |
* see the <code>doLayout</code> method in <code>JTable</code>. |
|
537 |
* |
|
538 |
* @param preferredWidth the new preferred width |
|
539 |
* @see #getPreferredWidth |
|
540 |
* @see JTable#doLayout() |
|
541 |
* @beaninfo |
|
542 |
* bound: true |
|
543 |
* description: The preferred width of the column. |
|
544 |
*/ |
|
545 |
public void setPreferredWidth(int preferredWidth) { |
|
546 |
int old = this.preferredWidth; |
|
547 |
this.preferredWidth = Math.min(Math.max(preferredWidth, minWidth), maxWidth); |
|
548 |
firePropertyChange("preferredWidth", old, this.preferredWidth); |
|
549 |
} |
|
550 |
||
551 |
/** |
|
552 |
* Returns the preferred width of the <code>TableColumn</code>. |
|
553 |
* The default preferred width is 75. |
|
554 |
* |
|
555 |
* @return the <code>preferredWidth</code> property |
|
556 |
* @see #setPreferredWidth |
|
557 |
*/ |
|
558 |
public int getPreferredWidth() { |
|
559 |
return preferredWidth; |
|
560 |
} |
|
561 |
||
562 |
/** |
|
563 |
* Sets the <code>TableColumn</code>'s minimum width to |
|
564 |
* <code>minWidth</code>, |
|
565 |
* adjusting the new minimum width if necessary to ensure that |
|
566 |
* 0 <= <code>minWidth</code> <= <code>maxWidth</code>. |
|
567 |
* For example, if the <code>minWidth</code> argument is negative, |
|
568 |
* this method sets the <code>minWidth</code> property to 0. |
|
569 |
* |
|
570 |
* <p> |
|
571 |
* If the value of the |
|
572 |
* <code>width</code> or <code>preferredWidth</code> property |
|
573 |
* is less than the new minimum width, |
|
574 |
* this method sets that property to the new minimum width. |
|
575 |
* |
|
576 |
* @param minWidth the new minimum width |
|
577 |
* @see #getMinWidth |
|
578 |
* @see #setPreferredWidth |
|
579 |
* @see #setMaxWidth |
|
580 |
* @beaninfo |
|
581 |
* bound: true |
|
582 |
* description: The minimum width of the column. |
|
583 |
*/ |
|
584 |
public void setMinWidth(int minWidth) { |
|
585 |
int old = this.minWidth; |
|
586 |
this.minWidth = Math.max(Math.min(minWidth, maxWidth), 0); |
|
587 |
if (width < this.minWidth) { |
|
588 |
setWidth(this.minWidth); |
|
589 |
} |
|
590 |
if (preferredWidth < this.minWidth) { |
|
591 |
setPreferredWidth(this.minWidth); |
|
592 |
} |
|
593 |
firePropertyChange("minWidth", old, this.minWidth); |
|
594 |
} |
|
595 |
||
596 |
/** |
|
597 |
* Returns the minimum width for the <code>TableColumn</code>. The |
|
598 |
* <code>TableColumn</code>'s width can't be made less than this either |
|
599 |
* by the user or programmatically. |
|
600 |
* |
|
601 |
* @return the <code>minWidth</code> property |
|
602 |
* @see #setMinWidth |
|
603 |
* @see #TableColumn(int, int, TableCellRenderer, TableCellEditor) |
|
604 |
*/ |
|
605 |
public int getMinWidth() { |
|
606 |
return minWidth; |
|
607 |
} |
|
608 |
||
609 |
/** |
|
610 |
* Sets the <code>TableColumn</code>'s maximum width to |
|
611 |
* <code>maxWidth</code> or, |
|
612 |
* if <code>maxWidth</code> is less than the minimum width, |
|
613 |
* to the minimum width. |
|
614 |
* |
|
615 |
* <p> |
|
616 |
* If the value of the |
|
617 |
* <code>width</code> or <code>preferredWidth</code> property |
|
618 |
* is more than the new maximum width, |
|
619 |
* this method sets that property to the new maximum width. |
|
620 |
* |
|
621 |
* @param maxWidth the new maximum width |
|
622 |
* @see #getMaxWidth |
|
623 |
* @see #setPreferredWidth |
|
624 |
* @see #setMinWidth |
|
625 |
* @beaninfo |
|
626 |
* bound: true |
|
627 |
* description: The maximum width of the column. |
|
628 |
*/ |
|
629 |
public void setMaxWidth(int maxWidth) { |
|
630 |
int old = this.maxWidth; |
|
631 |
this.maxWidth = Math.max(minWidth, maxWidth); |
|
632 |
if (width > this.maxWidth) { |
|
633 |
setWidth(this.maxWidth); |
|
634 |
} |
|
635 |
if (preferredWidth > this.maxWidth) { |
|
636 |
setPreferredWidth(this.maxWidth); |
|
637 |
} |
|
638 |
firePropertyChange("maxWidth", old, this.maxWidth); |
|
639 |
} |
|
640 |
||
641 |
/** |
|
642 |
* Returns the maximum width for the <code>TableColumn</code>. The |
|
643 |
* <code>TableColumn</code>'s width can't be made larger than this |
|
644 |
* either by the user or programmatically. The default maxWidth |
|
645 |
* is Integer.MAX_VALUE. |
|
646 |
* |
|
647 |
* @return the <code>maxWidth</code> property |
|
648 |
* @see #setMaxWidth |
|
649 |
*/ |
|
650 |
public int getMaxWidth() { |
|
651 |
return maxWidth; |
|
652 |
} |
|
653 |
||
654 |
/** |
|
655 |
* Sets whether this column can be resized. |
|
656 |
* |
|
657 |
* @param isResizable if true, resizing is allowed; otherwise false |
|
658 |
* @see #getResizable |
|
659 |
* @beaninfo |
|
660 |
* bound: true |
|
661 |
* description: Whether or not this column can be resized. |
|
662 |
*/ |
|
663 |
public void setResizable(boolean isResizable) { |
|
664 |
boolean old = this.isResizable; |
|
665 |
this.isResizable = isResizable; |
|
666 |
firePropertyChange("isResizable", old, this.isResizable); |
|
667 |
} |
|
668 |
||
669 |
/** |
|
670 |
* Returns true if the user is allowed to resize the |
|
671 |
* <code>TableColumn</code>'s |
|
672 |
* width, false otherwise. You can change the width programmatically |
|
673 |
* regardless of this setting. The default is true. |
|
674 |
* |
|
675 |
* @return the <code>isResizable</code> property |
|
676 |
* @see #setResizable |
|
677 |
*/ |
|
678 |
public boolean getResizable() { |
|
679 |
return isResizable; |
|
680 |
} |
|
681 |
||
682 |
/** |
|
683 |
* Resizes the <code>TableColumn</code> to fit the width of its header cell. |
|
684 |
* This method does nothing if the header renderer is <code>null</code> |
|
685 |
* (the default case). Otherwise, it sets the minimum, maximum and preferred |
|
686 |
* widths of this column to the widths of the minimum, maximum and preferred |
|
687 |
* sizes of the Component delivered by the header renderer. |
|
688 |
* The transient "width" property of this TableColumn is also set to the |
|
689 |
* preferred width. Note this method is not used internally by the table |
|
690 |
* package. |
|
691 |
* |
|
692 |
* @see #setPreferredWidth |
|
693 |
*/ |
|
694 |
public void sizeWidthToFit() { |
|
695 |
if (headerRenderer == null) { |
|
696 |
return; |
|
697 |
} |
|
698 |
Component c = headerRenderer.getTableCellRendererComponent(null, |
|
699 |
getHeaderValue(), false, false, 0, 0); |
|
700 |
||
701 |
setMinWidth(c.getMinimumSize().width); |
|
702 |
setMaxWidth(c.getMaximumSize().width); |
|
703 |
setPreferredWidth(c.getPreferredSize().width); |
|
704 |
||
705 |
setWidth(getPreferredWidth()); |
|
706 |
} |
|
707 |
||
708 |
/** |
|
709 |
* This field was not used in previous releases and there are |
|
710 |
* currently no plans to support it in the future. |
|
711 |
* |
|
712 |
* @deprecated as of Java 2 platform v1.3 |
|
713 |
*/ |
|
714 |
@Deprecated |
|
715 |
public void disableResizedPosting() { |
|
716 |
resizedPostingDisableCount++; |
|
717 |
} |
|
718 |
||
719 |
/** |
|
720 |
* This field was not used in previous releases and there are |
|
721 |
* currently no plans to support it in the future. |
|
722 |
* |
|
723 |
* @deprecated as of Java 2 platform v1.3 |
|
724 |
*/ |
|
725 |
@Deprecated |
|
726 |
public void enableResizedPosting() { |
|
727 |
resizedPostingDisableCount--; |
|
728 |
} |
|
729 |
||
730 |
// |
|
731 |
// Property Change Support |
|
732 |
// |
|
733 |
||
734 |
/** |
|
735 |
* Adds a <code>PropertyChangeListener</code> to the listener list. |
|
736 |
* The listener is registered for all properties. |
|
737 |
* <p> |
|
738 |
* A <code>PropertyChangeEvent</code> will get fired in response to an |
|
739 |
* explicit call to <code>setFont</code>, <code>setBackground</code>, |
|
740 |
* or <code>setForeground</code> on the |
|
741 |
* current component. Note that if the current component is |
|
742 |
* inheriting its foreground, background, or font from its |
|
743 |
* container, then no event will be fired in response to a |
|
744 |
* change in the inherited property. |
|
745 |
* |
|
746 |
* @param listener the listener to be added |
|
747 |
* |
|
748 |
*/ |
|
749 |
public synchronized void addPropertyChangeListener( |
|
750 |
PropertyChangeListener listener) { |
|
751 |
if (changeSupport == null) { |
|
752 |
changeSupport = new SwingPropertyChangeSupport(this); |
|
753 |
} |
|
754 |
changeSupport.addPropertyChangeListener(listener); |
|
755 |
} |
|
756 |
||
757 |
/** |
|
758 |
* Removes a <code>PropertyChangeListener</code> from the listener list. |
|
759 |
* The <code>PropertyChangeListener</code> to be removed was registered |
|
760 |
* for all properties. |
|
761 |
* |
|
762 |
* @param listener the listener to be removed |
|
763 |
* |
|
764 |
*/ |
|
765 |
||
766 |
public synchronized void removePropertyChangeListener( |
|
767 |
PropertyChangeListener listener) { |
|
768 |
if (changeSupport != null) { |
|
769 |
changeSupport.removePropertyChangeListener(listener); |
|
770 |
} |
|
771 |
} |
|
772 |
||
773 |
/** |
|
774 |
* Returns an array of all the <code>PropertyChangeListener</code>s added |
|
775 |
* to this TableColumn with addPropertyChangeListener(). |
|
776 |
* |
|
777 |
* @return all of the <code>PropertyChangeListener</code>s added or an empty |
|
778 |
* array if no listeners have been added |
|
779 |
* @since 1.4 |
|
780 |
*/ |
|
781 |
public synchronized PropertyChangeListener[] getPropertyChangeListeners() { |
|
782 |
if (changeSupport == null) { |
|
783 |
return new PropertyChangeListener[0]; |
|
784 |
} |
|
785 |
return changeSupport.getPropertyChangeListeners(); |
|
786 |
} |
|
787 |
||
788 |
// |
|
789 |
// Protected Methods |
|
790 |
// |
|
791 |
||
792 |
/** |
|
793 |
* As of Java 2 platform v1.3, this method is not called by the <code>TableColumn</code> |
|
794 |
* constructor. Previously this method was used by the |
|
795 |
* <code>TableColumn</code> to create a default header renderer. |
|
796 |
* As of Java 2 platform v1.3, the default header renderer is <code>null</code>. |
|
797 |
* <code>JTableHeader</code> now provides its own shared default |
|
798 |
* renderer, just as the <code>JTable</code> does for its cell renderers. |
|
799 |
* |
|
800 |
* @return the default header renderer |
|
801 |
* @see javax.swing.table.JTableHeader#createDefaultRenderer() |
|
802 |
*/ |
|
803 |
protected TableCellRenderer createDefaultHeaderRenderer() { |
|
804 |
DefaultTableCellRenderer label = new DefaultTableCellRenderer() { |
|
805 |
public Component getTableCellRendererComponent(JTable table, Object value, |
|
806 |
boolean isSelected, boolean hasFocus, int row, int column) { |
|
807 |
if (table != null) { |
|
808 |
JTableHeader header = table.getTableHeader(); |
|
809 |
if (header != null) { |
|
810 |
setForeground(header.getForeground()); |
|
811 |
setBackground(header.getBackground()); |
|
812 |
setFont(header.getFont()); |
|
813 |
} |
|
814 |
} |
|
815 |
||
816 |
setText((value == null) ? "" : value.toString()); |
|
817 |
setBorder(UIManager.getBorder("TableHeader.cellBorder")); |
|
818 |
return this; |
|
819 |
} |
|
820 |
}; |
|
821 |
label.setHorizontalAlignment(JLabel.CENTER); |
|
822 |
return label; |
|
823 |
} |
|
824 |
||
825 |
} // End of class TableColumn |