1 /* 2 * Copyright (c) 1997, 2017, 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 java.awt.Component; 29 import java.beans.BeanProperty; 30 import java.beans.PropertyChangeListener; 31 import java.io.Serializable; 32 33 import javax.swing.JLabel; 34 import javax.swing.JTable; 35 import javax.swing.UIManager; 36 import javax.swing.event.SwingPropertyChangeSupport; 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 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 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 static final 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 static final 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 static final 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 static final 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 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 protected transient 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 */ 313 @BeanProperty(description 314 = "The model index.") 315 public void setModelIndex(int modelIndex) { 316 int old = this.modelIndex; 317 this.modelIndex = modelIndex; 318 firePropertyChange("modelIndex", old, modelIndex); 319 } 320 321 /** 322 * Returns the model index for this column. 323 * @return the <code>modelIndex</code> property 324 */ 325 public int getModelIndex() { 326 return modelIndex; 327 } 328 329 /** 330 * Sets the <code>TableColumn</code>'s identifier to 331 * <code>anIdentifier</code>. <p> 332 * Note: identifiers are not used by the <code>JTable</code>, 333 * they are purely a 334 * convenience for the external tagging and location of columns. 335 * 336 * @param identifier an identifier for this column 337 * @see #getIdentifier 338 */ 339 @BeanProperty(description 340 = "A unique identifier for this column.") 341 public void setIdentifier(Object identifier) { 342 Object old = this.identifier; 343 this.identifier = identifier; 344 firePropertyChange("identifier", old, identifier); 345 } 346 347 348 /** 349 * Returns the <code>identifier</code> object for this column. 350 * Note identifiers are not used by <code>JTable</code>, 351 * they are purely a convenience for external use. 352 * If the <code>identifier</code> is <code>null</code>, 353 * <code>getIdentifier()</code> returns <code>getHeaderValue</code> 354 * as a default. 355 * 356 * @return the <code>identifier</code> property 357 * @see #setIdentifier 358 */ 359 public Object getIdentifier() { 360 return (identifier != null) ? identifier : getHeaderValue(); 361 362 } 363 364 /** 365 * Sets the <code>Object</code> whose string representation will be 366 * used as the value for the <code>headerRenderer</code>. When the 367 * <code>TableColumn</code> is created, the default <code>headerValue</code> 368 * is <code>null</code>. 369 * @param headerValue the new headerValue 370 * @see #getHeaderValue 371 */ 372 @BeanProperty(description 373 = "The text to be used by the header renderer.") 374 public void setHeaderValue(Object headerValue) { 375 Object old = this.headerValue; 376 this.headerValue = headerValue; 377 firePropertyChange("headerValue", old, headerValue); 378 } 379 380 /** 381 * Returns the <code>Object</code> used as the value for the header 382 * renderer. 383 * 384 * @return the <code>headerValue</code> property 385 * @see #setHeaderValue 386 */ 387 public Object getHeaderValue() { 388 return headerValue; 389 } 390 391 // 392 // Renderers and Editors 393 // 394 395 /** 396 * Sets the <code>TableCellRenderer</code> used to draw the 397 * <code>TableColumn</code>'s header to <code>headerRenderer</code>. 398 * <p> 399 * It is the header renderers responsibility to render the sorting 400 * indicator. If you are using sorting and specify a renderer your 401 * renderer must render the sorting indication. 402 * 403 * @param headerRenderer the new headerRenderer 404 * 405 * @see #getHeaderRenderer 406 */ 407 @BeanProperty(description 408 = "The header renderer.") 409 public void setHeaderRenderer(TableCellRenderer headerRenderer) { 410 TableCellRenderer old = this.headerRenderer; 411 this.headerRenderer = headerRenderer; 412 firePropertyChange("headerRenderer", old, headerRenderer); 413 } 414 415 /** 416 * Returns the <code>TableCellRenderer</code> used to draw the header of the 417 * <code>TableColumn</code>. When the <code>headerRenderer</code> is 418 * <code>null</code>, the <code>JTableHeader</code> 419 * uses its <code>defaultRenderer</code>. The default value for a 420 * <code>headerRenderer</code> is <code>null</code>. 421 * 422 * @return the <code>headerRenderer</code> property 423 * @see #setHeaderRenderer 424 * @see #setHeaderValue 425 * @see javax.swing.table.JTableHeader#getDefaultRenderer() 426 */ 427 public TableCellRenderer getHeaderRenderer() { 428 return headerRenderer; 429 } 430 431 /** 432 * Sets the <code>TableCellRenderer</code> used by <code>JTable</code> 433 * to draw individual values for this column. 434 * 435 * @param cellRenderer the new cellRenderer 436 * @see #getCellRenderer 437 */ 438 @BeanProperty(description 439 = "The renderer to use for cell values.") 440 public void setCellRenderer(TableCellRenderer cellRenderer) { 441 TableCellRenderer old = this.cellRenderer; 442 this.cellRenderer = cellRenderer; 443 firePropertyChange("cellRenderer", old, cellRenderer); 444 } 445 446 /** 447 * Returns the <code>TableCellRenderer</code> used by the 448 * <code>JTable</code> to draw 449 * values for this column. The <code>cellRenderer</code> of the column 450 * not only controls the visual look for the column, but is also used to 451 * interpret the value object supplied by the <code>TableModel</code>. 452 * When the <code>cellRenderer</code> is <code>null</code>, 453 * the <code>JTable</code> uses a default renderer based on the 454 * class of the cells in that column. The default value for a 455 * <code>cellRenderer</code> is <code>null</code>. 456 * 457 * @return the <code>cellRenderer</code> property 458 * @see #setCellRenderer 459 * @see JTable#setDefaultRenderer 460 */ 461 public TableCellRenderer getCellRenderer() { 462 return cellRenderer; 463 } 464 465 /** 466 * Sets the editor to used by when a cell in this column is edited. 467 * 468 * @param cellEditor the new cellEditor 469 * @see #getCellEditor 470 */ 471 @BeanProperty(description 472 = "The editor to use for cell values.") 473 public void setCellEditor(TableCellEditor cellEditor){ 474 TableCellEditor old = this.cellEditor; 475 this.cellEditor = cellEditor; 476 firePropertyChange("cellEditor", old, cellEditor); 477 } 478 479 /** 480 * Returns the <code>TableCellEditor</code> used by the 481 * <code>JTable</code> to edit values for this column. When the 482 * <code>cellEditor</code> is <code>null</code>, the <code>JTable</code> 483 * uses a default editor based on the 484 * class of the cells in that column. The default value for a 485 * <code>cellEditor</code> is <code>null</code>. 486 * 487 * @return the <code>cellEditor</code> property 488 * @see #setCellEditor 489 * @see JTable#setDefaultEditor 490 */ 491 public TableCellEditor getCellEditor() { 492 return cellEditor; 493 } 494 495 /** 496 * This method should not be used to set the widths of columns in the 497 * <code>JTable</code>, use <code>setPreferredWidth</code> instead. 498 * Like a layout manager in the 499 * AWT, the <code>JTable</code> adjusts a column's width automatically 500 * whenever the 501 * table itself changes size, or a column's preferred width is changed. 502 * Setting widths programmatically therefore has no long term effect. 503 * <p> 504 * This method sets this column's width to <code>width</code>. 505 * If <code>width</code> exceeds the minimum or maximum width, 506 * it is adjusted to the appropriate limiting value. 507 * @param width the new width 508 * @see #getWidth 509 * @see #setMinWidth 510 * @see #setMaxWidth 511 * @see #setPreferredWidth 512 * @see JTable#doLayout() 513 */ 514 @BeanProperty(description 515 = "The width of the column.") 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 */ 547 @BeanProperty(description 548 = "The preferred width of the column.") 549 public void setPreferredWidth(int preferredWidth) { 550 int old = this.preferredWidth; 551 this.preferredWidth = Math.min(Math.max(preferredWidth, minWidth), maxWidth); 552 firePropertyChange("preferredWidth", old, this.preferredWidth); 553 } 554 555 /** 556 * Returns the preferred width of the <code>TableColumn</code>. 557 * The default preferred width is 75. 558 * 559 * @return the <code>preferredWidth</code> property 560 * @see #setPreferredWidth 561 */ 562 public int getPreferredWidth() { 563 return preferredWidth; 564 } 565 566 /** 567 * Sets the <code>TableColumn</code>'s minimum width to 568 * <code>minWidth</code>, 569 * adjusting the new minimum width if necessary to ensure that 570 * 0 <= <code>minWidth</code> <= <code>maxWidth</code>. 571 * For example, if the <code>minWidth</code> argument is negative, 572 * this method sets the <code>minWidth</code> property to 0. 573 * 574 * <p> 575 * If the value of the 576 * <code>width</code> or <code>preferredWidth</code> property 577 * is less than the new minimum width, 578 * this method sets that property to the new minimum width. 579 * 580 * @param minWidth the new minimum width 581 * @see #getMinWidth 582 * @see #setPreferredWidth 583 * @see #setMaxWidth 584 */ 585 @BeanProperty(description 586 = "The minimum width of the column.") 587 public void setMinWidth(int minWidth) { 588 int old = this.minWidth; 589 this.minWidth = Math.max(Math.min(minWidth, maxWidth), 0); 590 if (width < this.minWidth) { 591 setWidth(this.minWidth); 592 } 593 if (preferredWidth < this.minWidth) { 594 setPreferredWidth(this.minWidth); 595 } 596 firePropertyChange("minWidth", old, this.minWidth); 597 } 598 599 /** 600 * Returns the minimum width for the <code>TableColumn</code>. The 601 * <code>TableColumn</code>'s width can't be made less than this either 602 * by the user or programmatically. 603 * 604 * @return the <code>minWidth</code> property 605 * @see #setMinWidth 606 * @see #TableColumn(int, int, TableCellRenderer, TableCellEditor) 607 */ 608 public int getMinWidth() { 609 return minWidth; 610 } 611 612 /** 613 * Sets the <code>TableColumn</code>'s maximum width to 614 * <code>maxWidth</code> or, 615 * if <code>maxWidth</code> is less than the minimum width, 616 * to the minimum width. 617 * 618 * <p> 619 * If the value of the 620 * <code>width</code> or <code>preferredWidth</code> property 621 * is more than the new maximum width, 622 * this method sets that property to the new maximum width. 623 * 624 * @param maxWidth the new maximum width 625 * @see #getMaxWidth 626 * @see #setPreferredWidth 627 * @see #setMinWidth 628 */ 629 @BeanProperty(description 630 = "The maximum width of the column.") 631 public void setMaxWidth(int maxWidth) { 632 int old = this.maxWidth; 633 this.maxWidth = Math.max(minWidth, maxWidth); 634 if (width > this.maxWidth) { 635 setWidth(this.maxWidth); 636 } 637 if (preferredWidth > this.maxWidth) { 638 setPreferredWidth(this.maxWidth); 639 } 640 firePropertyChange("maxWidth", old, this.maxWidth); 641 } 642 643 /** 644 * Returns the maximum width for the <code>TableColumn</code>. The 645 * <code>TableColumn</code>'s width can't be made larger than this 646 * either by the user or programmatically. The default maxWidth 647 * is Integer.MAX_VALUE. 648 * 649 * @return the <code>maxWidth</code> property 650 * @see #setMaxWidth 651 */ 652 public int getMaxWidth() { 653 return maxWidth; 654 } 655 656 /** 657 * Sets whether this column can be resized. 658 * 659 * @param isResizable if true, resizing is allowed; otherwise false 660 * @see #getResizable 661 */ 662 @BeanProperty(description 663 = "Whether or not this column can be resized.") 664 public void setResizable(boolean isResizable) { 665 boolean old = this.isResizable; 666 this.isResizable = isResizable; 667 firePropertyChange("isResizable", old, this.isResizable); 668 } 669 670 /** 671 * Returns true if the user is allowed to resize the 672 * <code>TableColumn</code>'s 673 * width, false otherwise. You can change the width programmatically 674 * regardless of this setting. The default is true. 675 * 676 * @return the <code>isResizable</code> property 677 * @see #setResizable 678 */ 679 public boolean getResizable() { 680 return isResizable; 681 } 682 683 /** 684 * Resizes the <code>TableColumn</code> to fit the width of its header cell. 685 * This method does nothing if the header renderer is <code>null</code> 686 * (the default case). Otherwise, it sets the minimum, maximum and preferred 687 * widths of this column to the widths of the minimum, maximum and preferred 688 * sizes of the Component delivered by the header renderer. 689 * The transient "width" property of this TableColumn is also set to the 690 * preferred width. Note this method is not used internally by the table 691 * package. 692 * 693 * @see #setPreferredWidth 694 */ 695 public void sizeWidthToFit() { 696 if (headerRenderer == null) { 697 return; 698 } 699 Component c = headerRenderer.getTableCellRendererComponent(null, 700 getHeaderValue(), false, false, 0, 0); 701 702 setMinWidth(c.getMinimumSize().width); 703 setMaxWidth(c.getMaximumSize().width); 704 setPreferredWidth(c.getPreferredSize().width); 705 706 setWidth(getPreferredWidth()); 707 } 708 709 /** 710 * This field was not used in previous releases and there are 711 * currently no plans to support it in the future. 712 * 713 * @deprecated as of Java 2 platform v1.3 714 */ 715 @Deprecated 716 public void disableResizedPosting() { 717 resizedPostingDisableCount++; 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 enableResizedPosting() { 728 resizedPostingDisableCount--; 729 } 730 731 // 732 // Property Change Support 733 // 734 735 /** 736 * Adds a {@code PropertyChangeListener} to the listener list. The listener 737 * is registered for all bound properties of this class, including the 738 * following: 739 * <ul> 740 * <li>this TableColumn's modelIndex ("modelIndex")</li> 741 * <li>this TableColumn's identifier ("identifier")</li> 742 * <li>this TableColumn's header value ("headerValue")</li> 743 * <li>this TableColumn's header renderer ("headerRenderer")</li> 744 * <li>this TableColumn's cell renderer ("cellRenderer")</li> 745 * <li>this TableColumn's cell editor ("cellEditor")</li> 746 * <li>this TableColumn's width ("width")</li> 747 * <li>this TableColumn's preferred width ("preferredWidth")</li> 748 * <li>this TableColumn's minimum width ("minWidth")</li> 749 * <li>this TableColumn's maximum width ("maxWidth")</li> 750 * <li>this TableColumn's resizable state ("isResizable")</li> 751 * </ul> 752 * 753 * @param listener the listener to be added 754 * @see #removePropertyChangeListener(PropertyChangeListener) 755 */ 756 public synchronized void addPropertyChangeListener( 757 PropertyChangeListener listener) { 758 if (changeSupport == null) { 759 changeSupport = new SwingPropertyChangeSupport(this); 760 } 761 changeSupport.addPropertyChangeListener(listener); 762 } 763 764 /** 765 * Removes a <code>PropertyChangeListener</code> from the listener list. 766 * The <code>PropertyChangeListener</code> to be removed was registered 767 * for all properties. 768 * 769 * @param listener the listener to be removed 770 * 771 */ 772 773 public synchronized void removePropertyChangeListener( 774 PropertyChangeListener listener) { 775 if (changeSupport != null) { 776 changeSupport.removePropertyChangeListener(listener); 777 } 778 } 779 780 /** 781 * Returns an array of all the <code>PropertyChangeListener</code>s added 782 * to this TableColumn with addPropertyChangeListener(). 783 * 784 * @return all of the <code>PropertyChangeListener</code>s added or an empty 785 * array if no listeners have been added 786 * @since 1.4 787 */ 788 public synchronized PropertyChangeListener[] getPropertyChangeListeners() { 789 if (changeSupport == null) { 790 return new PropertyChangeListener[0]; 791 } 792 return changeSupport.getPropertyChangeListeners(); 793 } 794 795 // 796 // Protected Methods 797 // 798 799 /** 800 * As of Java 2 platform v1.3, this method is not called by the <code>TableColumn</code> 801 * constructor. Previously this method was used by the 802 * <code>TableColumn</code> to create a default header renderer. 803 * As of Java 2 platform v1.3, the default header renderer is <code>null</code>. 804 * <code>JTableHeader</code> now provides its own shared default 805 * renderer, just as the <code>JTable</code> does for its cell renderers. 806 * 807 * @return the default header renderer 808 * @see javax.swing.table.JTableHeader#createDefaultRenderer() 809 */ 810 protected TableCellRenderer createDefaultHeaderRenderer() { 811 DefaultTableCellRenderer label = new DefaultTableCellRenderer() { 812 public Component getTableCellRendererComponent(JTable table, Object value, 813 boolean isSelected, 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