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