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