1 /*
   2  * Copyright (c) 1997, 2014, Oracle and/or its affiliates. All rights reserved.
   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
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  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  *
  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.
  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
  40  *  <code>JTable</code>, such as width, resizability, minimum and maximum width.
  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
  70  * of all JavaBeans&trade;
  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 @SuppressWarnings("serial") // Same-version serialization only
  86 public class TableColumn extends Object implements Serializable {
  87 
  88     /**
  89      * Obsolete as of Java 2 platform v1.3.  Please use string literals to identify
  90      * properties.
  91      */
  92     /*
  93      * Warning: The value of this constant, "columWidth" is wrong as the
  94      * name of the property is "columnWidth".
  95      */
  96     public final static String COLUMN_WIDTH_PROPERTY = "columWidth";
  97 
  98     /**
  99      * 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      *  @param modelIndex  the index of the column in the model
 206      *  that supplies the data for this column in the table;
 207      *  the model index remains the same even when columns
 208      *  are reordered in the view
 209      */
 210     public TableColumn(int modelIndex) {
 211         this(modelIndex, 75, null, null);
 212     }
 213 
 214     /**
 215      *  Cover method, using a <code>null</code> renderer and a
 216      *  <code>null</code> editor.
 217      *  @see #TableColumn(int, int, TableCellRenderer, TableCellEditor)
 218      *
 219      *  @param modelIndex  the index of the column in the model
 220      *  that supplies the data for this column in the table;
 221      *  the model index remains the same even when columns
 222      *  are reordered in the view
 223      *  @param width  this column's preferred width and initial width
 224      */
 225     public TableColumn(int modelIndex, int width) {
 226         this(modelIndex, width, null, null);
 227     }
 228 
 229     /**
 230      *  Creates and initializes an instance of
 231      *  <code>TableColumn</code> with the specified model index,
 232      *  width, cell renderer, and cell editor;
 233      *  all <code>TableColumn</code> constructors delegate to this one.
 234      *  The value of <code>width</code> is used
 235      *  for both the initial and preferred width;
 236      *  if <code>width</code> is negative,
 237      *  they're set to 0.
 238      *  The minimum width is set to 15 unless the initial width is less,
 239      *  in which case the minimum width is set to
 240      *  the initial width.
 241      *
 242      *  <p>
 243      *  When the <code>cellRenderer</code>
 244      *  or <code>cellEditor</code> parameter is <code>null</code>,
 245      *  a default value provided by the <code>JTable</code>
 246      *  <code>getDefaultRenderer</code>
 247      *  or <code>getDefaultEditor</code> method, respectively,
 248      *  is used to
 249      *  provide defaults based on the type of the data in this column.
 250      *  This column-centric rendering strategy can be circumvented by overriding
 251      *  the <code>getCellRenderer</code> methods in <code>JTable</code>.
 252      *
 253      * @param modelIndex the index of the column
 254      *  in the model that supplies the data for this column in the table;
 255      *  the model index remains the same
 256      *  even when columns are reordered in the view
 257      * @param width this column's preferred width and initial width
 258      * @param cellRenderer the object used to render values in this column
 259      * @param cellEditor the object used to edit values in this column
 260      * @see #getMinWidth()
 261      * @see JTable#getDefaultRenderer(Class)
 262      * @see JTable#getDefaultEditor(Class)
 263      * @see JTable#getCellRenderer(int, int)
 264      * @see JTable#getCellEditor(int, int)
 265      */
 266     public TableColumn(int modelIndex, int width,
 267                                  TableCellRenderer cellRenderer,
 268                                  TableCellEditor cellEditor) {
 269         super();
 270         this.modelIndex = modelIndex;
 271         preferredWidth = this.width = Math.max(width, 0);
 272 
 273         this.cellRenderer = cellRenderer;
 274         this.cellEditor = cellEditor;
 275 
 276         // Set other instance variables to default values.
 277         minWidth = Math.min(15, this.width);
 278         maxWidth = Integer.MAX_VALUE;
 279         isResizable = true;
 280         resizedPostingDisableCount = 0;
 281         headerValue = null;
 282     }
 283 
 284 //
 285 // Modifying and Querying attributes
 286 //
 287 
 288     private void firePropertyChange(String propertyName, Object oldValue, Object newValue) {
 289         if (changeSupport != null) {
 290             changeSupport.firePropertyChange(propertyName, oldValue, newValue);
 291         }
 292     }
 293 
 294     private void firePropertyChange(String propertyName, int oldValue, int newValue) {
 295         if (oldValue != newValue) {
 296             firePropertyChange(propertyName, Integer.valueOf(oldValue), Integer.valueOf(newValue));
 297         }
 298     }
 299 
 300     private void firePropertyChange(String propertyName, boolean oldValue, boolean newValue) {
 301         if (oldValue != newValue) {
 302             firePropertyChange(propertyName, Boolean.valueOf(oldValue), Boolean.valueOf(newValue));
 303         }
 304     }
 305 
 306     /**
 307      * Sets the model index for this column. The model index is the
 308      * index of the column in the model that will be displayed by this
 309      * <code>TableColumn</code>. As the <code>TableColumn</code>
 310      * is moved around in the view the model index remains constant.
 311      * @param  modelIndex  the new modelIndex
 312      * @beaninfo
 313      *  bound: true
 314      *  description: The model index.
 315      */
 316     public void setModelIndex(int modelIndex) {
 317         int old = this.modelIndex;
 318         this.modelIndex = modelIndex;
 319         firePropertyChange("modelIndex", old, modelIndex);
 320     }
 321 
 322     /**
 323      * Returns the model index for this column.
 324      * @return the <code>modelIndex</code> property
 325      */
 326     public int getModelIndex() {
 327         return modelIndex;
 328     }
 329 
 330     /**
 331      * Sets the <code>TableColumn</code>'s identifier to
 332      * <code>anIdentifier</code>. <p>
 333      * Note: identifiers are not used by the <code>JTable</code>,
 334      * they are purely a
 335      * convenience for the external tagging and location of columns.
 336      *
 337      * @param      identifier           an identifier for this column
 338      * @see        #getIdentifier
 339      * @beaninfo
 340      *  bound: true
 341      *  description: A unique identifier for this column.
 342      */
 343     public void setIdentifier(Object identifier) {
 344         Object old = this.identifier;
 345         this.identifier = identifier;
 346         firePropertyChange("identifier", old, identifier);
 347     }
 348 
 349 
 350     /**
 351      *  Returns the <code>identifier</code> object for this column.
 352      *  Note identifiers are not used by <code>JTable</code>,
 353      *  they are purely a convenience for external use.
 354      *  If the <code>identifier</code> is <code>null</code>,
 355      *  <code>getIdentifier()</code> returns <code>getHeaderValue</code>
 356      *  as a default.
 357      *
 358      * @return  the <code>identifier</code> property
 359      * @see     #setIdentifier
 360      */
 361     public Object getIdentifier() {
 362         return (identifier != null) ? identifier : getHeaderValue();
 363 
 364     }
 365 
 366     /**
 367      * Sets the <code>Object</code> whose string representation will be
 368      * used as the value for the <code>headerRenderer</code>.  When the
 369      * <code>TableColumn</code> is created, the default <code>headerValue</code>
 370      * is <code>null</code>.
 371      * @param headerValue  the new headerValue
 372      * @see       #getHeaderValue
 373      * @beaninfo
 374      *  bound: true
 375      *  description: The text to be used by the header renderer.
 376      */
 377     public void setHeaderValue(Object headerValue) {
 378         Object old = this.headerValue;
 379         this.headerValue = headerValue;
 380         firePropertyChange("headerValue", old, headerValue);
 381     }
 382 
 383     /**
 384      * Returns the <code>Object</code> used as the value for the header
 385      * renderer.
 386      *
 387      * @return  the <code>headerValue</code> property
 388      * @see     #setHeaderValue
 389      */
 390     public Object getHeaderValue() {
 391         return headerValue;
 392     }
 393 
 394     //
 395     // Renderers and Editors
 396     //
 397 
 398     /**
 399      * Sets the <code>TableCellRenderer</code> used to draw the
 400      * <code>TableColumn</code>'s header to <code>headerRenderer</code>.
 401      * <p>
 402      * It is the header renderers responsibility to render the sorting
 403      * indicator.  If you are using sorting and specify a renderer your
 404      * renderer must render the sorting indication.
 405      *
 406      * @param headerRenderer  the new headerRenderer
 407      *
 408      * @see       #getHeaderRenderer
 409      * @beaninfo
 410      *  bound: true
 411      *  description: The header renderer.
 412      */
 413     public void setHeaderRenderer(TableCellRenderer headerRenderer) {
 414         TableCellRenderer old = this.headerRenderer;
 415         this.headerRenderer = headerRenderer;
 416         firePropertyChange("headerRenderer", old, headerRenderer);
 417     }
 418 
 419     /**
 420      * Returns the <code>TableCellRenderer</code> used to draw the header of the
 421      * <code>TableColumn</code>. When the <code>headerRenderer</code> is
 422      * <code>null</code>, the <code>JTableHeader</code>
 423      * uses its <code>defaultRenderer</code>. The default value for a
 424      * <code>headerRenderer</code> is <code>null</code>.
 425      *
 426      * @return  the <code>headerRenderer</code> property
 427      * @see     #setHeaderRenderer
 428      * @see     #setHeaderValue
 429      * @see     javax.swing.table.JTableHeader#getDefaultRenderer()
 430      */
 431     public TableCellRenderer getHeaderRenderer() {
 432         return headerRenderer;
 433     }
 434 
 435     /**
 436      * Sets the <code>TableCellRenderer</code> used by <code>JTable</code>
 437      * to draw individual values for this column.
 438      *
 439      * @param cellRenderer  the new cellRenderer
 440      * @see     #getCellRenderer
 441      * @beaninfo
 442      *  bound: true
 443      *  description: The renderer to use for cell values.
 444      */
 445     public void setCellRenderer(TableCellRenderer cellRenderer) {
 446         TableCellRenderer old = this.cellRenderer;
 447         this.cellRenderer = cellRenderer;
 448         firePropertyChange("cellRenderer", old, cellRenderer);
 449     }
 450 
 451     /**
 452      * Returns the <code>TableCellRenderer</code> used by the
 453      * <code>JTable</code> to draw
 454      * values for this column.  The <code>cellRenderer</code> of the column
 455      * not only controls the visual look for the column, but is also used to
 456      * interpret the value object supplied by the <code>TableModel</code>.
 457      * When the <code>cellRenderer</code> is <code>null</code>,
 458      * the <code>JTable</code> uses a default renderer based on the
 459      * class of the cells in that column. The default value for a
 460      * <code>cellRenderer</code> is <code>null</code>.
 461      *
 462      * @return  the <code>cellRenderer</code> property
 463      * @see     #setCellRenderer
 464      * @see     JTable#setDefaultRenderer
 465      */
 466     public TableCellRenderer getCellRenderer() {
 467         return cellRenderer;
 468     }
 469 
 470     /**
 471      * Sets the editor to used by when a cell in this column is edited.
 472      *
 473      * @param cellEditor  the new cellEditor
 474      * @see     #getCellEditor
 475      * @beaninfo
 476      *  bound: true
 477      *  description: The editor to use for cell values.
 478      */
 479     public void setCellEditor(TableCellEditor cellEditor){
 480         TableCellEditor old = this.cellEditor;
 481         this.cellEditor = cellEditor;
 482         firePropertyChange("cellEditor", old, cellEditor);
 483     }
 484 
 485     /**
 486      * Returns the <code>TableCellEditor</code> used by the
 487      * <code>JTable</code> to edit values for this column.  When the
 488      * <code>cellEditor</code> is <code>null</code>, the <code>JTable</code>
 489      * uses a default editor based on the
 490      * class of the cells in that column. The default value for a
 491      * <code>cellEditor</code> is <code>null</code>.
 492      *
 493      * @return  the <code>cellEditor</code> property
 494      * @see     #setCellEditor
 495      * @see     JTable#setDefaultEditor
 496      */
 497     public TableCellEditor getCellEditor() {
 498         return cellEditor;
 499     }
 500 
 501     /**
 502      * This method should not be used to set the widths of columns in the
 503      * <code>JTable</code>, use <code>setPreferredWidth</code> instead.
 504      * Like a layout manager in the
 505      * AWT, the <code>JTable</code> adjusts a column's width automatically
 506      * whenever the
 507      * table itself changes size, or a column's preferred width is changed.
 508      * Setting widths programmatically therefore has no long term effect.
 509      * <p>
 510      * This method sets this column's width to <code>width</code>.
 511      * If <code>width</code> exceeds the minimum or maximum width,
 512      * it is adjusted to the appropriate limiting value.
 513      * @param  width  the new width
 514      * @see     #getWidth
 515      * @see     #setMinWidth
 516      * @see     #setMaxWidth
 517      * @see     #setPreferredWidth
 518      * @see     JTable#doLayout()
 519      * @beaninfo
 520      *  bound: true
 521      *  description: The width of the column.
 522      */
 523     public void setWidth(int width) {
 524         int old = this.width;
 525         this.width = Math.min(Math.max(width, minWidth), maxWidth);
 526         firePropertyChange("width", old, this.width);
 527     }
 528 
 529     /**
 530      * Returns the width of the <code>TableColumn</code>. The default width is
 531      * 75.
 532      *
 533      * @return  the <code>width</code> property
 534      * @see     #setWidth
 535      */
 536     public int getWidth() {
 537         return width;
 538     }
 539 
 540     /**
 541      * Sets this column's preferred width to <code>preferredWidth</code>.
 542      * If <code>preferredWidth</code> exceeds the minimum or maximum width,
 543      * it is adjusted to the appropriate limiting value.
 544      * <p>
 545      * For details on how the widths of columns in the <code>JTable</code>
 546      * (and <code>JTableHeader</code>) are calculated from the
 547      * <code>preferredWidth</code>,
 548      * see the <code>doLayout</code> method in <code>JTable</code>.
 549      *
 550      * @param  preferredWidth the new preferred width
 551      * @see     #getPreferredWidth
 552      * @see     JTable#doLayout()
 553      * @beaninfo
 554      *  bound: true
 555      *  description: The preferred width of the column.
 556      */
 557     public void setPreferredWidth(int preferredWidth) {
 558         int old = this.preferredWidth;
 559         this.preferredWidth = Math.min(Math.max(preferredWidth, minWidth), maxWidth);
 560         firePropertyChange("preferredWidth", old, this.preferredWidth);
 561     }
 562 
 563     /**
 564      * Returns the preferred width of the <code>TableColumn</code>.
 565      * The default preferred width is 75.
 566      *
 567      * @return  the <code>preferredWidth</code> property
 568      * @see     #setPreferredWidth
 569      */
 570     public int getPreferredWidth() {
 571         return preferredWidth;
 572     }
 573 
 574     /**
 575      * Sets the <code>TableColumn</code>'s minimum width to
 576      * <code>minWidth</code>,
 577      * adjusting the new minimum width if necessary to ensure that
 578      * 0 &lt;= <code>minWidth</code> &lt;= <code>maxWidth</code>.
 579      * For example, if the <code>minWidth</code> argument is negative,
 580      * this method sets the <code>minWidth</code> property to 0.
 581      *
 582      * <p>
 583      * If the value of the
 584      * <code>width</code> or <code>preferredWidth</code> property
 585      * is less than the new minimum width,
 586      * this method sets that property to the new minimum width.
 587      *
 588      * @param minWidth  the new minimum width
 589      * @see     #getMinWidth
 590      * @see     #setPreferredWidth
 591      * @see     #setMaxWidth
 592      * @beaninfo
 593      *  bound: true
 594      *  description: The minimum width of the column.
 595      */
 596     public void setMinWidth(int minWidth) {
 597         int old = this.minWidth;
 598         this.minWidth = Math.max(Math.min(minWidth, maxWidth), 0);
 599         if (width < this.minWidth) {
 600             setWidth(this.minWidth);
 601         }
 602         if (preferredWidth < this.minWidth) {
 603             setPreferredWidth(this.minWidth);
 604         }
 605         firePropertyChange("minWidth", old, this.minWidth);
 606     }
 607 
 608     /**
 609      * Returns the minimum width for the <code>TableColumn</code>. The
 610      * <code>TableColumn</code>'s width can't be made less than this either
 611      * by the user or programmatically.
 612      *
 613      * @return  the <code>minWidth</code> property
 614      * @see     #setMinWidth
 615      * @see     #TableColumn(int, int, TableCellRenderer, TableCellEditor)
 616      */
 617     public int getMinWidth() {
 618         return minWidth;
 619     }
 620 
 621     /**
 622      * Sets the <code>TableColumn</code>'s maximum width to
 623      * <code>maxWidth</code> or,
 624      * if <code>maxWidth</code> is less than the minimum width,
 625      * to the minimum width.
 626      *
 627      * <p>
 628      * If the value of the
 629      * <code>width</code> or <code>preferredWidth</code> property
 630      * is more than the new maximum width,
 631      * this method sets that property to the new maximum width.
 632      *
 633      * @param maxWidth  the new maximum width
 634      * @see     #getMaxWidth
 635      * @see     #setPreferredWidth
 636      * @see     #setMinWidth
 637      * @beaninfo
 638      *  bound: true
 639      *  description: The maximum width of the column.
 640      */
 641     public void setMaxWidth(int maxWidth) {
 642         int old = this.maxWidth;
 643         this.maxWidth = Math.max(minWidth, maxWidth);
 644         if (width > this.maxWidth) {
 645             setWidth(this.maxWidth);
 646         }
 647         if (preferredWidth > this.maxWidth) {
 648             setPreferredWidth(this.maxWidth);
 649         }
 650         firePropertyChange("maxWidth", old, this.maxWidth);
 651     }
 652 
 653     /**
 654      * Returns the maximum width for the <code>TableColumn</code>. The
 655      * <code>TableColumn</code>'s width can't be made larger than this
 656      * either by the user or programmatically.  The default maxWidth
 657      * is Integer.MAX_VALUE.
 658      *
 659      * @return  the <code>maxWidth</code> property
 660      * @see     #setMaxWidth
 661      */
 662     public int getMaxWidth() {
 663         return maxWidth;
 664     }
 665 
 666     /**
 667      * Sets whether this column can be resized.
 668      *
 669      * @param isResizable  if true, resizing is allowed; otherwise false
 670      * @see     #getResizable
 671      * @beaninfo
 672      *  bound: true
 673      *  description: Whether or not this column can be resized.
 674      */
 675     public void setResizable(boolean isResizable) {
 676         boolean old = this.isResizable;
 677         this.isResizable = isResizable;
 678         firePropertyChange("isResizable", old, this.isResizable);
 679     }
 680 
 681     /**
 682      * Returns true if the user is allowed to resize the
 683      * <code>TableColumn</code>'s
 684      * width, false otherwise. You can change the width programmatically
 685      * regardless of this setting.  The default is true.
 686      *
 687      * @return  the <code>isResizable</code> property
 688      * @see     #setResizable
 689      */
 690     public boolean getResizable() {
 691         return isResizable;
 692     }
 693 
 694     /**
 695      * Resizes the <code>TableColumn</code> to fit the width of its header cell.
 696      * This method does nothing if the header renderer is <code>null</code>
 697      * (the default case). Otherwise, it sets the minimum, maximum and preferred
 698      * widths of this column to the widths of the minimum, maximum and preferred
 699      * sizes of the Component delivered by the header renderer.
 700      * The transient "width" property of this TableColumn is also set to the
 701      * preferred width. Note this method is not used internally by the table
 702      * package.
 703      *
 704      * @see     #setPreferredWidth
 705      */
 706     public void sizeWidthToFit() {
 707         if (headerRenderer == null) {
 708             return;
 709         }
 710         Component c = headerRenderer.getTableCellRendererComponent(null,
 711                                 getHeaderValue(), false, false, 0, 0);
 712 
 713         setMinWidth(c.getMinimumSize().width);
 714         setMaxWidth(c.getMaximumSize().width);
 715         setPreferredWidth(c.getPreferredSize().width);
 716 
 717         setWidth(getPreferredWidth());
 718     }
 719 
 720     /**
 721      * This field was not used in previous releases and there are
 722      * currently no plans to support it in the future.
 723      *
 724      * @deprecated as of Java 2 platform v1.3
 725      */
 726     @Deprecated
 727     public void disableResizedPosting() {
 728         resizedPostingDisableCount++;
 729     }
 730 
 731     /**
 732      * This field was not used in previous releases and there are
 733      * currently no plans to support it in the future.
 734      *
 735      * @deprecated as of Java 2 platform v1.3
 736      */
 737     @Deprecated
 738     public void enableResizedPosting() {
 739         resizedPostingDisableCount--;
 740     }
 741 
 742 //
 743 // Property Change Support
 744 //
 745 
 746     /**
 747      * Adds a <code>PropertyChangeListener</code> to the listener list.
 748      * The listener is registered for all properties.
 749      * <p>
 750      * A <code>PropertyChangeEvent</code> will get fired in response to an
 751      * explicit call to <code>setFont</code>, <code>setBackground</code>,
 752      * or <code>setForeground</code> on the
 753      * current component.  Note that if the current component is
 754      * inheriting its foreground, background, or font from its
 755      * container, then no event will be fired in response to a
 756      * change in the inherited property.
 757      *
 758      * @param listener  the listener to be added
 759      *
 760      */
 761     public synchronized void addPropertyChangeListener(
 762                                 PropertyChangeListener listener) {
 763         if (changeSupport == null) {
 764             changeSupport = new SwingPropertyChangeSupport(this);
 765         }
 766         changeSupport.addPropertyChangeListener(listener);
 767     }
 768 
 769     /**
 770      * Removes a <code>PropertyChangeListener</code> from the listener list.
 771      * The <code>PropertyChangeListener</code> to be removed was registered
 772      * for all properties.
 773      *
 774      * @param listener  the listener to be removed
 775      *
 776      */
 777 
 778     public synchronized void removePropertyChangeListener(
 779                                 PropertyChangeListener listener) {
 780         if (changeSupport != null) {
 781             changeSupport.removePropertyChangeListener(listener);
 782         }
 783     }
 784 
 785     /**
 786      * Returns an array of all the <code>PropertyChangeListener</code>s added
 787      * to this TableColumn with addPropertyChangeListener().
 788      *
 789      * @return all of the <code>PropertyChangeListener</code>s added or an empty
 790      *         array if no listeners have been added
 791      * @since 1.4
 792      */
 793     public synchronized PropertyChangeListener[] getPropertyChangeListeners() {
 794         if (changeSupport == null) {
 795             return new PropertyChangeListener[0];
 796         }
 797         return changeSupport.getPropertyChangeListeners();
 798     }
 799 
 800 //
 801 // Protected Methods
 802 //
 803 
 804     /**
 805      * As of Java 2 platform v1.3, this method is not called by the <code>TableColumn</code>
 806      * constructor.  Previously this method was used by the
 807      * <code>TableColumn</code> to create a default header renderer.
 808      * As of Java 2 platform v1.3, the default header renderer is <code>null</code>.
 809      * <code>JTableHeader</code> now provides its own shared default
 810      * renderer, just as the <code>JTable</code> does for its cell renderers.
 811      *
 812      * @return the default header renderer
 813      * @see javax.swing.table.JTableHeader#createDefaultRenderer()
 814      */
 815     protected TableCellRenderer createDefaultHeaderRenderer() {
 816         DefaultTableCellRenderer label = new DefaultTableCellRenderer() {
 817             public Component getTableCellRendererComponent(JTable table, Object value,
 818                          boolean isSelected, boolean hasFocus, int row, int column) {
 819                 if (table != null) {
 820                     JTableHeader header = table.getTableHeader();
 821                     if (header != null) {
 822                         setForeground(header.getForeground());
 823                         setBackground(header.getBackground());
 824                         setFont(header.getFont());
 825                     }
 826                 }
 827 
 828                 setText((value == null) ? "" : value.toString());
 829                 setBorder(UIManager.getBorder("TableHeader.cellBorder"));
 830                 return this;
 831             }
 832         };
 833         label.setHorizontalAlignment(JLabel.CENTER);
 834         return label;
 835     }
 836 
 837 } // End of class TableColumn