1. /*
  2. * @(#)TableColumn.java 1.49 00/02/02
  3. *
  4. * Copyright 1997-2000 Sun Microsystems, Inc. All Rights Reserved.
  5. *
  6. * This software is the proprietary information of Sun Microsystems, Inc.
  7. * Use is subject to license terms.
  8. *
  9. */
  10. package javax.swing.table;
  11. import javax.swing.*;
  12. import javax.swing.border.*;
  13. import javax.swing.event.SwingPropertyChangeSupport;
  14. import java.lang.Integer;
  15. import java.awt.Color;
  16. import java.awt.Component;
  17. import java.io.Serializable;
  18. import java.beans.PropertyChangeEvent;
  19. import java.beans.PropertyChangeListener;
  20. /**
  21. * A <code>TableColumn</code> represents all the attributes of a column in a
  22. * <code>JTable</code>, such as width, resizibility, minimum and maximum width.
  23. * In addition, the <code>TableColumn</code> provides slots for a renderer and
  24. * an editor that can be used to display and edit the values in this column.
  25. * <p>
  26. * It is also possible to specify renderers and editors on a per type basis
  27. * rather than a per column basis - see the
  28. * <code>setDefaultRenderer</code> method in the <code>JTable</code> class.
  29. * This default mechanism is only used when the renderer (or
  30. * editor) in the <code>TableColumn</code> is <code>null</code>.
  31. * <p>
  32. * The <code>TableColumn</code> stores the link between the columns in the
  33. * <code>JTable</code> and the columns in the <code>TableModel</code>.
  34. * The <code>modelIndex</code> is the column in the
  35. * <code>TableModel</code>, which will be queried for the data values for the
  36. * cells in this column. As the column moves around in the view this
  37. * <code>modelIndex</code> does not change.
  38. * <p>
  39. * <strong>Warning:</strong>
  40. * Serialized objects of this class will not be compatible with
  41. * future Swing releases. The current serialization support is appropriate
  42. * for short term storage or RMI between applications running the same
  43. * version of Swing. A future release of Swing will provide support for
  44. * long term persistence.
  45. *
  46. * @version 1.49 02/02/00
  47. * @author Alan Chung
  48. * @author Philip Milne
  49. * @see javax.swing.table.TableColumnModel
  50. *
  51. * @see javax.swing.table.DefaultTableColumnModel
  52. * @see javax.swing.table.JTableHeader#getDefaultRenderer()
  53. * @see JTable#getDefaultRenderer(Class)
  54. * @see JTable#getDefaultEditor(Class)
  55. * @see JTable#getCellRenderer(int, int)
  56. * @see JTable#getCellEditor(int, int)
  57. */
  58. public class TableColumn extends Object implements Serializable {
  59. /**
  60. * Obsolete as of Java 2 platform v1.3. Please use string literals to identify
  61. * properties.
  62. */
  63. /*
  64. * Warning: The value of this constant, "columWidth" is wrong as the
  65. * name of the property is "columnWidth".
  66. */
  67. public final static String COLUMN_WIDTH_PROPERTY = "columWidth";
  68. /**
  69. * Obsolete as of Java 2 platform v1.3. Please use string literals to identify
  70. * properties.
  71. */
  72. public final static String HEADER_VALUE_PROPERTY = "headerValue";
  73. /**
  74. * Obsolete as of Java 2 platform v1.3. Please use string literals to identify
  75. * properties.
  76. */
  77. public final static String HEADER_RENDERER_PROPERTY = "headerRenderer";
  78. /**
  79. * Obsolete as of Java 2 platform v1.3. Please use string literals to identify
  80. * properties.
  81. */
  82. public final static String CELL_RENDERER_PROPERTY = "cellRenderer";
  83. //
  84. // Instance Variables
  85. //
  86. /**
  87. * The index of the column in the model which is to be displayed by
  88. * this <code>TableColumn</code>. As columns are moved around in the
  89. * view <code>modelIndex</code> remains constant.
  90. */
  91. protected int modelIndex;
  92. /**
  93. * This object is not used internally by the drawing machinery of
  94. * the <code>JTable</code> identifiers may be set in the
  95. * <code>TableColumn</code> as as an
  96. * optional way to tag and locate table columns. The table package does
  97. * not modify or invoke any methods in these identifer objects other
  98. * than the <code>equals</code> method which is used in the
  99. * <code>getColumnIndex()</code> method in the
  100. * <code>DefaultTableColumnModel</code>.
  101. */
  102. protected Object identifier;
  103. /** The width of the column. */
  104. protected int width;
  105. /** The minimum width of the column. */
  106. protected int minWidth;
  107. /** The preferred width of the column. */
  108. private int preferredWidth;
  109. /** The maximum width of the column. */
  110. protected int maxWidth;
  111. /** The renderer used to draw the header of the column. */
  112. protected TableCellRenderer headerRenderer;
  113. /** The header value of the column. */
  114. protected Object headerValue;
  115. /** The renderer used to draw the data cells of the column. */
  116. protected TableCellRenderer cellRenderer;
  117. /** The editor used to edit the data cells of the column. */
  118. protected TableCellEditor cellEditor;
  119. /** If true, the user is allowed to resize the column; the default is true. */
  120. protected boolean isResizable;
  121. /**
  122. * This field was not used in previous releases and there are
  123. * currently no plans to support it in the future.
  124. *
  125. * @deprecated as of Java 2 platform v1.3
  126. */
  127. /*
  128. * Counter used to disable posting of resizing notifications until the
  129. * end of the resize.
  130. */
  131. transient protected int resizedPostingDisableCount;
  132. /**
  133. * If any <code>PropertyChangeListeners</code> have been registered, the
  134. * <code>changeSupport</code> field describes them.
  135. */
  136. private SwingPropertyChangeSupport changeSupport;
  137. //
  138. // Constructors
  139. //
  140. /**
  141. * Cover method, using a default model index of 0,
  142. * default width of 75, a <code>null</code> renderer and a
  143. * <code>null</code> editor.
  144. * This method is intended for serialization.
  145. * @see #TableColumn(int, int, TableCellRenderer, TableCellEditor)
  146. */
  147. public TableColumn() {
  148. this(0);
  149. }
  150. /**
  151. * Cover method, using a default width of 75, a <code>null</code>
  152. * renderer and a <code>null</code> editor.
  153. * @see #TableColumn(int, int, TableCellRenderer, TableCellEditor)
  154. */
  155. public TableColumn(int modelIndex) {
  156. this(modelIndex, 75, null, null);
  157. }
  158. /**
  159. * Cover method, using a <code>null</code> renderer and a
  160. * <code>null</code> editor.
  161. * @see #TableColumn(int, int, TableCellRenderer, TableCellEditor)
  162. */
  163. public TableColumn(int modelIndex, int width) {
  164. this(modelIndex, width, null, null);
  165. }
  166. /**
  167. * Creates and initializes an instance of
  168. * <code>TableColumn</code> with <code>modelIndex</code>.
  169. * All <code>TableColumn</code> constructors delegate to this one.
  170. * The <code>modelIndex</code> is the index of the column
  171. * in the model which will supply the data for this column in the table.
  172. * The <code>modelIndex</code> does not change as the columns are reordered
  173. * in the view. The width parameter is used to set both the
  174. * <code>preferredWidth</code> for this
  175. * column and the initial width. The renderer and editor are the objects
  176. * used respectively to render and edit values in this column. When
  177. * these are <code>null</code>, default values, provided by the
  178. * <code>getDefaultRenderer</code>
  179. * and <code>getDefaultEditor</code> methods in the
  180. * <code>JTable</code> class are used to
  181. * provide defaults based on the type of the data in this column.
  182. * This column-centric rendering strategy can be circumvented by overriding
  183. * the <code>getCellRenderer</code> methods in the <code>JTable</code>.
  184. * <p>
  185. *
  186. * @see JTable#getDefaultRenderer(Class)
  187. * @see JTable#getDefaultEditor(Class)
  188. * @see JTable#getCellRenderer(int, int)
  189. * @see JTable#getCellEditor(int, int)
  190. */
  191. public TableColumn(int modelIndex, int width,
  192. TableCellRenderer cellRenderer,
  193. TableCellEditor cellEditor) {
  194. super();
  195. this.modelIndex = modelIndex;
  196. this.width = width;
  197. this.preferredWidth = width;
  198. this.cellRenderer = cellRenderer;
  199. this.cellEditor = cellEditor;
  200. // Set other instance variables to default values.
  201. minWidth = 15;
  202. maxWidth = Integer.MAX_VALUE;
  203. isResizable = true;
  204. resizedPostingDisableCount = 0;
  205. headerValue = null;
  206. }
  207. //
  208. // Modifying and Querying attributes
  209. //
  210. private void firePropertyChange(String propertyName, Object oldValue, Object newValue) {
  211. if (changeSupport != null) {
  212. changeSupport.firePropertyChange(propertyName, oldValue, newValue);
  213. }
  214. }
  215. private void firePropertyChange(String propertyName, int oldValue, int newValue) {
  216. if (oldValue != newValue) {
  217. firePropertyChange(propertyName, new Integer(oldValue), new Integer(newValue));
  218. }
  219. }
  220. private void firePropertyChange(String propertyName, boolean oldValue, boolean newValue) {
  221. if (oldValue != newValue) {
  222. firePropertyChange(propertyName, new Boolean(oldValue), new Boolean(newValue));
  223. }
  224. }
  225. /**
  226. * Sets the model index for this column. The model index is the
  227. * index of the column in the model that will be displayed by this
  228. * <code>TableColumn</code>. As the <code>TableColumn</code>
  229. * is moved around in the view the model index remains constant.
  230. * @param modelIndex the new modelIndex
  231. * @beaninfo
  232. * bound: true
  233. * description: The model index.
  234. */
  235. public void setModelIndex(int modelIndex) {
  236. int old = this.modelIndex;
  237. this.modelIndex = modelIndex;
  238. firePropertyChange("modelIndex", old, modelIndex);
  239. }
  240. /**
  241. * Returns the model index for this column.
  242. * @return the <code>modelIndex</code> property
  243. */
  244. public int getModelIndex() {
  245. return modelIndex;
  246. }
  247. /**
  248. * Sets the <code>TableColumn</code>'s identifier to
  249. * <code>anIdentifier</code>. <p>
  250. * Note: identifiers are not used by the <code>JTable</code>,
  251. * they are purely a
  252. * convenience for the external tagging and location of columns.
  253. *
  254. * @param identifier an identifier for this column
  255. * @see #getIdentifier
  256. * @beaninfo
  257. * bound: true
  258. * description: A unique identifier for this column.
  259. */
  260. public void setIdentifier(Object identifier) {
  261. Object old = this.identifier;
  262. this.identifier = identifier;
  263. firePropertyChange("identifier", old, identifier);
  264. }
  265. /**
  266. * Returns the <code>identifier</code> object for this column.
  267. * Note identifiers are not used by <code>JTable</code>,
  268. * they are purely a convenience for external use.
  269. * If the <code>identifier</code> is <code>null</code>,
  270. * <code>getIdentifier()</code> returns <code>getHeaderValue</code>
  271. * as a default.
  272. *
  273. * @return the <code>identifier</code> property
  274. * @see #setIdentifier
  275. */
  276. public Object getIdentifier() {
  277. return (identifier != null) ? identifier : getHeaderValue();
  278. }
  279. /**
  280. * Sets the <code>Object</code> whose string representation will be
  281. * used as the value for the <code>headerRenderer</code>. When the
  282. * <code>TableColumn</code> is created, the default <code>headerValue</code>
  283. * is <code>null</code>.
  284. * @param headerValue the new headerValue
  285. * @see #getHeaderValue
  286. * @beaninfo
  287. * bound: true
  288. * description: The text to be used by the header renderer.
  289. */
  290. public void setHeaderValue(Object headerValue) {
  291. Object old = this.headerValue;
  292. this.headerValue = headerValue;
  293. firePropertyChange("headerValue", old, headerValue);
  294. }
  295. /**
  296. * Returns the <code>Object</code> used as the value for the header
  297. * renderer.
  298. *
  299. * @return the <code>headerValue</code> property
  300. * @see #setHeaderValue
  301. */
  302. public Object getHeaderValue() {
  303. return headerValue;
  304. }
  305. //
  306. // Renderers and Editors
  307. //
  308. /**
  309. * Sets the <code>TableCellRenderer</code> used to draw the
  310. * <code>TableColumn</code>'s header to <code>headerRenderer</code>.
  311. *
  312. * @param headerRenderer the new headerRenderer
  313. *
  314. * @see #getHeaderRenderer
  315. * @beaninfo
  316. * bound: true
  317. * description: The header renderer.
  318. */
  319. public void setHeaderRenderer(TableCellRenderer headerRenderer) {
  320. TableCellRenderer old = this.headerRenderer;
  321. this.headerRenderer = headerRenderer;
  322. firePropertyChange("headerRenderer", old, headerRenderer);
  323. }
  324. /**
  325. * Returns the <code>TableCellRenderer</code> used to draw the header of the
  326. * <code>TableColumn</code>. When the <code>headerRenderer</code> is
  327. * <code>null</code>, the <code>JTableHeader</code>
  328. * uses its <code>defaultRenderer</code>. The default value for a
  329. * <code>headerRenderer</code> is <code>null</code>.
  330. *
  331. * @return the <code>headerRenderer</code> property
  332. * @see #setHeaderRenderer
  333. * @see #setHeaderValue
  334. * @see javax.swing.table.JTableHeader#getDefaultRenderer()
  335. */
  336. public TableCellRenderer getHeaderRenderer() {
  337. return headerRenderer;
  338. }
  339. /**
  340. * Sets the <code>TableCellRenderer</code> used by <code>JTable</code>
  341. * to draw individual values for this column.
  342. *
  343. * @param cellRenderer the new cellRenderer
  344. * @see #getCellRenderer
  345. * @beaninfo
  346. * bound: true
  347. * description: The renderer to use for cell values.
  348. */
  349. public void setCellRenderer(TableCellRenderer cellRenderer) {
  350. TableCellRenderer old = this.cellRenderer;
  351. this.cellRenderer = cellRenderer;
  352. firePropertyChange("cellRenderer", old, cellRenderer);
  353. }
  354. /**
  355. * Returns the <code>TableCellRenderer</code> used by the
  356. * <code>JTable</code> to draw
  357. * values for this column. The <code>cellRenderer</code> of the column
  358. * not only controls the visual look for the column, but is also used to
  359. * interpret the value object supplied by the <code>TableModel</code>.
  360. * When the <code>cellRenderer</code> is <code>null</code>,
  361. * the <code>JTable</code> uses a default renderer based on the
  362. * class of the cells in that column. The default value for a
  363. * <code>cellRenderer</code> is <code>null</code>.
  364. *
  365. * @return the <code>cellRenderer</code> property
  366. * @see #setCellRenderer
  367. * @see JTable#setDefaultRenderer
  368. */
  369. public TableCellRenderer getCellRenderer() {
  370. return cellRenderer;
  371. }
  372. /**
  373. * Sets the editor to used by when a cell in this column is edited.
  374. *
  375. * @param cellEditor the new cellEditor
  376. * @see #getCellEditor
  377. * @beaninfo
  378. * bound: true
  379. * description: The editor to use for cell values.
  380. */
  381. public void setCellEditor(TableCellEditor cellEditor){
  382. TableCellEditor old = this.cellEditor;
  383. this.cellEditor = cellEditor;
  384. firePropertyChange("cellEditor", old, cellEditor);
  385. }
  386. /**
  387. * Returns the <code>TableCellEditor</code> used by the
  388. * <code>JTable</code> to edit values for this column. When the
  389. * <code>cellEditor</code> is <code>null</code>, the <code>JTable</code>
  390. * uses a default editor based on the
  391. * class of the cells in that column. The default value for a
  392. * <code>cellEditor</code> is <code>null</code>.
  393. *
  394. * @return the <code>cellEditor</code> property
  395. * @see #setCellEditor
  396. * @see JTable#setDefaultEditor
  397. */
  398. public TableCellEditor getCellEditor() {
  399. return cellEditor;
  400. }
  401. /**
  402. * This method should not be used to set the widths of columns in the
  403. * <code>JTable</code>, use <code>setPreferredWidth</code> instead.
  404. * Like a layout manager in the
  405. * AWT, the <code>JTable</code> adjusts a column's width automatically
  406. * whenever the
  407. * table itself changes size, or a column's preferred width is changed.
  408. * Setting widths programmatically therefore has no long term effect.
  409. * <p>
  410. * This method sets this column's width to <code>width</code>.
  411. * If <code>width</code> exceeds the minimum or maximum width,
  412. * it is adjusted to the appropriate limiting value.
  413. * <p>
  414. * @param width the new width
  415. * @see #getWidth
  416. * @see #setMinWidth
  417. * @see #setMaxWidth
  418. * @see #setPreferredWidth
  419. * @see JTable#sizeColumnsToFit(int)
  420. * @beaninfo
  421. * bound: true
  422. * description: The width of the column.
  423. */
  424. public void setWidth(int width) {
  425. int old = this.width;
  426. this.width = Math.min(Math.max(width, minWidth), maxWidth);
  427. firePropertyChange("width", old, this.width);
  428. }
  429. /**
  430. * Returns the width of the <code>TableColumn</code>. The default width is
  431. * 75.
  432. *
  433. * @return the <code>width</code> property
  434. * @see #setWidth
  435. */
  436. public int getWidth() {
  437. return width;
  438. }
  439. /**
  440. * Sets this column's preferred width to <code>preferredWidth</code>.
  441. * If <code>preferredWidth</code> exceeds the minimum or maximum width,
  442. * it is adjusted to the appropriate limiting value.
  443. * <p>
  444. * For details on how the widths of columns in the <code>JTable</code>
  445. * (and <code>JTableHeader</code>) are calculated from the
  446. * <code>preferredWidth</code>,
  447. * see the <code>sizeColumnsToFit</code> method in <code>JTable</code>.
  448. *
  449. * @param preferredWidth the new preferred width
  450. * @see #getPreferredWidth
  451. * @see JTable#sizeColumnsToFit(int)
  452. * @beaninfo
  453. * bound: true
  454. * description: The preferred width of the column.
  455. */
  456. public void setPreferredWidth(int preferredWidth) {
  457. int old = this.preferredWidth;
  458. this.preferredWidth = Math.min(Math.max(preferredWidth, minWidth), maxWidth);
  459. firePropertyChange("preferredWidth", old, this.preferredWidth);
  460. }
  461. /**
  462. * Returns the preferred width of the <code>TableColumn</code>.
  463. * The default preferred width is 75.
  464. *
  465. * @return the <code>preferredWidth</code> property
  466. * @see #setPreferredWidth
  467. */
  468. public int getPreferredWidth() {
  469. return preferredWidth;
  470. }
  471. /**
  472. * Sets the <code>TableColumn</code>'s minimum width to
  473. * <code>minWidth</code> also adjusts the current width
  474. * and preferred width if they are less than this value.
  475. *
  476. * @param minWidth the new minimum width
  477. * @see #getMinWidth
  478. * @see #setPreferredWidth
  479. * @see #setMaxWidth
  480. * @beaninfo
  481. * bound: true
  482. * description: The minimum width of the column.
  483. */
  484. public void setMinWidth(int minWidth) {
  485. int old = this.minWidth;
  486. this.minWidth = Math.max(minWidth, 0);
  487. if (width < minWidth) {
  488. setWidth(minWidth);
  489. }
  490. if (preferredWidth < minWidth) {
  491. setPreferredWidth(minWidth);
  492. }
  493. firePropertyChange("minWidth", old, this.minWidth);
  494. }
  495. /**
  496. * Returns the minimum width for the <code>TableColumn</code>. The
  497. * <code>TableColumn</code>'s width can't be made less than this either
  498. * by the user or programmatically. The default minWidth is 15.
  499. *
  500. * @return the <code>minWidth</code> property
  501. * @see #setMinWidth
  502. */
  503. public int getMinWidth() {
  504. return minWidth;
  505. }
  506. /**
  507. * Sets the <code>TableColumn</code>'s maximum width to
  508. * <code>maxWidth</code> also adjusts the width and preferred
  509. * width if they are greater than this value.
  510. *
  511. * @param maxWidth the new maximum width
  512. * @see #getMaxWidth
  513. * @see #setPreferredWidth
  514. * @see #setMinWidth
  515. * @beaninfo
  516. * bound: true
  517. * description: The maximum width of the column.
  518. */
  519. public void setMaxWidth(int maxWidth) {
  520. int old = this.maxWidth;
  521. this.maxWidth = Math.max(minWidth, maxWidth);
  522. if (width > maxWidth) {
  523. setWidth(maxWidth);
  524. }
  525. if (preferredWidth > maxWidth) {
  526. setPreferredWidth(maxWidth);
  527. }
  528. firePropertyChange("maxWidth", old, this.maxWidth);
  529. }
  530. /**
  531. * Returns the maximum width for the <code>TableColumn</code>. The
  532. * <code>TableColumn</code>'s width can't be made larger than this
  533. * either by the user or programmatically. The default maxWidth
  534. * is Integer.MAX_VALUE.
  535. *
  536. * @return the <code>maxWidth</code> property
  537. * @see #setMaxWidth
  538. */
  539. public int getMaxWidth() {
  540. return maxWidth;
  541. }
  542. /**
  543. * Sets whether this column can be resized.
  544. *
  545. * @param isResizable if true, resizing is allowed; otherwise false
  546. * @see #getResizable
  547. * @beaninfo
  548. * bound: true
  549. * description: Whether or not this column can be resized.
  550. */
  551. public void setResizable(boolean isResizable) {
  552. boolean old = this.isResizable;
  553. this.isResizable = isResizable;
  554. firePropertyChange("isResizable", old, this.isResizable);
  555. }
  556. /**
  557. * Returns true if the user is allowed to resize the
  558. * <code>TableColumn</code>'s
  559. * width, false otherwise. You can change the width programmatically
  560. * regardless of this setting. The default is true.
  561. *
  562. * @return the <code>isResizable</code> property
  563. * @see #setResizable
  564. */
  565. public boolean getResizable() {
  566. return isResizable;
  567. }
  568. /**
  569. * Resizes the <code>TableColumn</code> to fit the width of its header cell.
  570. * This method does nothing if the header renderer is <code>null</code>
  571. * (the default case).
  572. *
  573. * @see #setPreferredWidth
  574. */
  575. public void sizeWidthToFit() {
  576. // Get the preferred width of the header
  577. if (headerRenderer == null) {
  578. return;
  579. }
  580. Component comp = headerRenderer.getTableCellRendererComponent(null,
  581. getHeaderValue(), false, false, 0, 0);
  582. int headerWidth = comp.getPreferredSize().width;
  583. // Set the width
  584. this.setWidth(headerWidth);
  585. }
  586. /**
  587. * This field was not used in previous releases and there are
  588. * currently no plans to support it in the future.
  589. *
  590. * @deprecated as of Java 2 platform v1.3
  591. */
  592. public void disableResizedPosting() {
  593. resizedPostingDisableCount++;
  594. }
  595. /**
  596. * This field was not used in previous releases and there are
  597. * currently no plans to support it in the future.
  598. *
  599. * @deprecated as of Java 2 platform v1.3
  600. */
  601. public void enableResizedPosting() {
  602. resizedPostingDisableCount--;
  603. }
  604. //
  605. // Property Change Support
  606. //
  607. /**
  608. * Adds a <code>PropertyChangeListener</code> to the listener list.
  609. * The listener is registered for all properties.
  610. * <p>
  611. * A <code>PropertyChangeEvent</code> will get fired in response to an
  612. * explicit call to <code>setFont</code>, <code>setBackground</code>,
  613. * or <code>setForeground</code> on the
  614. * current component. Note that if the current component is
  615. * inheriting its foreground, background, or font from its
  616. * container, then no event will be fired in response to a
  617. * change in the inherited property.
  618. *
  619. * @param listener the listener to be added
  620. *
  621. */
  622. public synchronized void addPropertyChangeListener(
  623. PropertyChangeListener listener) {
  624. if (changeSupport == null) {
  625. changeSupport = new SwingPropertyChangeSupport(this);
  626. }
  627. changeSupport.addPropertyChangeListener(listener);
  628. }
  629. /**
  630. * Removes a <code>PropertyChangeListener</code> from the listener list.
  631. * The <code>PropertyChangeListener</code> to be removed was registered
  632. * for all properties.
  633. *
  634. * @param listener the listener to be removed
  635. *
  636. */
  637. public synchronized void removePropertyChangeListener(
  638. PropertyChangeListener listener) {
  639. if (changeSupport != null) {
  640. changeSupport.removePropertyChangeListener(listener);
  641. }
  642. }
  643. //
  644. // Protected Methods
  645. //
  646. /**
  647. * As of Java 2 platform v1.3, this method is not called by the <code>TableColumn</code>
  648. * constructor. Previously this method was used by the
  649. * <code>TableColumn</code> to create a default header renderer.
  650. * As of Java 2 platform v1.3, the default header renderer is <code>null</code>.
  651. * <code>JTableHeader</code> now provides its own shared default
  652. * renderer, just as the <code>JTable</code> does for its cell renderers.
  653. *
  654. * @return the default header renderer
  655. * @see javax.swing.table.JTableHeader#createDefaultRenderer()
  656. */
  657. protected TableCellRenderer createDefaultHeaderRenderer() {
  658. DefaultTableCellRenderer label = new DefaultTableCellRenderer() {
  659. public Component getTableCellRendererComponent(JTable table, Object value,
  660. boolean isSelected, boolean hasFocus, int row, int column) {
  661. if (table != null) {
  662. JTableHeader header = table.getTableHeader();
  663. if (header != null) {
  664. setForeground(header.getForeground());
  665. setBackground(header.getBackground());
  666. setFont(header.getFont());
  667. }
  668. }
  669. setText((value == null) ? "" : value.toString());
  670. setBorder(UIManager.getBorder("TableHeader.cellBorder"));
  671. return this;
  672. }
  673. };
  674. label.setHorizontalAlignment(JLabel.CENTER);
  675. return label;
  676. }
  677. } // End of class TableColumn