1. /*
  2. * @(#)DefaultListCellRenderer.java 1.13 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;
  8. import javax.swing.*;
  9. import javax.swing.event.*;
  10. import javax.swing.border.*;
  11. import java.awt.Component;
  12. import java.awt.Color;
  13. import java.io.Serializable;
  14. /**
  15. * Renders an item in a list.
  16. * <p>
  17. * <strong>Warning:</strong>
  18. * Serialized objects of this class will not be compatible with
  19. * future Swing releases. The current serialization support is appropriate
  20. * for short term storage or RMI between applications running the same
  21. * version of Swing. A future release of Swing will provide support for
  22. * long term persistence.
  23. *
  24. * @version 1.13 11/29/01
  25. * @author Philip Milne
  26. * @author Hans Muller
  27. */
  28. public class DefaultListCellRenderer extends JLabel
  29. implements ListCellRenderer, Serializable
  30. {
  31. protected static Border noFocusBorder;
  32. /**
  33. * Constructs a default renderer object for an item
  34. * in a list.
  35. */
  36. public DefaultListCellRenderer() {
  37. super();
  38. noFocusBorder = new EmptyBorder(1, 1, 1, 1);
  39. setOpaque(true);
  40. setBorder(noFocusBorder);
  41. }
  42. public Component getListCellRendererComponent(
  43. JList list,
  44. Object value,
  45. int index,
  46. boolean isSelected,
  47. boolean cellHasFocus)
  48. {
  49. setComponentOrientation(list.getComponentOrientation());
  50. if (isSelected) {
  51. setBackground(list.getSelectionBackground());
  52. setForeground(list.getSelectionForeground());
  53. }
  54. else {
  55. setBackground(list.getBackground());
  56. setForeground(list.getForeground());
  57. }
  58. if (value instanceof Icon) {
  59. setIcon((Icon)value);
  60. }
  61. else {
  62. setText((value == null) ? "" : value.toString());
  63. }
  64. setEnabled(list.isEnabled());
  65. setFont(list.getFont());
  66. setBorder((cellHasFocus) ? UIManager.getBorder("List.focusCellHighlightBorder") : noFocusBorder);
  67. return this;
  68. }
  69. /**
  70. * A subclass of DefaultListCellRenderer that implements UIResource.
  71. * DefaultListCellRenderer doesn't implement UIResource
  72. * directly so that applications can safely override the
  73. * cellRenderer property with DefaultListCellRenderer subclasses.
  74. * <p>
  75. * <strong>Warning:</strong>
  76. * Serialized objects of this class will not be compatible with
  77. * future Swing releases. The current serialization support is appropriate
  78. * for short term storage or RMI between applications running the same
  79. * version of Swing. A future release of Swing will provide support for
  80. * long term persistence.
  81. */
  82. public static class UIResource extends DefaultListCellRenderer
  83. implements javax.swing.plaf.UIResource
  84. {
  85. }
  86. }