1. /*
  2. * @(#)DefaultTableCellRenderer.java 1.16 01/11/29
  3. *
  4. * Copyright 2002 Sun Microsystems, Inc. All rights reserved.
  5. * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
  6. */
  7. package javax.swing.table;
  8. import javax.swing.*;
  9. import javax.swing.table.TableCellRenderer;
  10. import javax.swing.border.*;
  11. import java.awt.Component;
  12. import java.awt.Color;
  13. import java.io.Serializable;
  14. /**
  15. * The standard class for rendering (displaying) individual cells
  16. * in a JTable.
  17. * <p>
  18. * <strong>Warning:</strong>
  19. * Serialized objects of this class will not be compatible with
  20. * future Swing releases. The current serialization support is appropriate
  21. * for short term storage or RMI between applications running the same
  22. * version of Swing. A future release of Swing will provide support for
  23. * long term persistence.
  24. *
  25. * @version 1.16 11/29/01
  26. * @author Philip Milne
  27. * @see JTable
  28. */
  29. public class DefaultTableCellRenderer extends JLabel
  30. implements TableCellRenderer, Serializable
  31. {
  32. protected static Border noFocusBorder;
  33. // We need a place to store the color the JLabel should be returned
  34. // to after its foreground and background colors have been set
  35. // to the selection background color.
  36. // These ivars will be made protected when their names are finalized.
  37. private Color unselectedForeground;
  38. private Color unselectedBackground;
  39. /**
  40. * Creates a default table cell renderer.
  41. */
  42. public DefaultTableCellRenderer() {
  43. super();
  44. noFocusBorder = new EmptyBorder(1, 2, 1, 2);
  45. setOpaque(true);
  46. setBorder(noFocusBorder);
  47. }
  48. /**
  49. * Overrides <code>JComponent.setForeground</code> to specify
  50. * the unselected-foreground color using the specified color.
  51. */
  52. public void setForeground(Color c) {
  53. super.setForeground(c);
  54. unselectedForeground = c;
  55. }
  56. /**
  57. * Overrides <code>JComponent.setForeground</code> to specify
  58. * the unselected-background color using the specified color.
  59. */
  60. public void setBackground(Color c) {
  61. super.setBackground(c);
  62. unselectedBackground = c;
  63. }
  64. /**
  65. * Notification from the UIManager that the L&F has changed.
  66. * Replaces the current UI object with the latest version from the
  67. * UIManager.
  68. *
  69. * @see JComponent#updateUI
  70. */
  71. public void updateUI() {
  72. super.updateUI();
  73. setForeground(null);
  74. setBackground(null);
  75. }
  76. // implements javax.swing.table.TableCellRenderer
  77. public Component getTableCellRendererComponent(JTable table, Object value,
  78. boolean isSelected, boolean hasFocus, int row, int column) {
  79. if (isSelected) {
  80. super.setForeground(table.getSelectionForeground());
  81. super.setBackground(table.getSelectionBackground());
  82. }
  83. else {
  84. super.setForeground((unselectedForeground != null) ? unselectedForeground
  85. : table.getForeground());
  86. super.setBackground((unselectedBackground != null) ? unselectedBackground
  87. : table.getBackground());
  88. }
  89. setFont(table.getFont());
  90. if (hasFocus) {
  91. setBorder( UIManager.getBorder("Table.focusCellHighlightBorder") );
  92. if (table.isCellEditable(row, column)) {
  93. super.setForeground( UIManager.getColor("Table.focusCellForeground") );
  94. super.setBackground( UIManager.getColor("Table.focusCellBackground") );
  95. }
  96. } else {
  97. setBorder(noFocusBorder);
  98. }
  99. setValue(value);
  100. return this;
  101. }
  102. protected void setValue(Object value) {
  103. setText((value == null) ? "" : value.toString());
  104. }
  105. /**
  106. * A subclass of DefaultTableCellRenderer that implements UIResource.
  107. * DefaultTableCellRenderer doesn't implement UIResource
  108. * directly so that applications can safely override the
  109. * cellRenderer property with DefaultTableCellRenderer subclasses.
  110. * <p>
  111. * <strong>Warning:</strong>
  112. * Serialized objects of this class will not be compatible with
  113. * future Swing releases. The current serialization support is appropriate
  114. * for short term storage or RMI between applications running the same
  115. * version of Swing. A future release of Swing will provide support for
  116. * long term persistence.
  117. */
  118. public static class UIResource extends DefaultTableCellRenderer
  119. implements javax.swing.plaf.UIResource
  120. {
  121. }
  122. }