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