1. /*
  2. * @(#)BasicComboBoxRenderer.java 1.20 03/12/19
  3. *
  4. * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
  5. * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
  6. */
  7. package javax.swing.plaf.basic;
  8. import javax.swing.*;
  9. import javax.swing.event.*;
  10. import javax.swing.border.*;
  11. import java.awt.*;
  12. import java.io.Serializable;
  13. /**
  14. * ComboBox renderer
  15. * <p>
  16. * <strong>Warning:</strong>
  17. * Serialized objects of this class will not be compatible with
  18. * future Swing releases. The current serialization support is
  19. * appropriate for short term storage or RMI between applications running
  20. * the same version of Swing. As of 1.4, support for long term storage
  21. * of all JavaBeans<sup><font size="-2">TM</font></sup>
  22. * has been added to the <code>java.beans</code> package.
  23. * Please see {@link java.beans.XMLEncoder}.
  24. *
  25. * @version 1.20 12/19/03
  26. * @author Arnaud Weber
  27. */
  28. public class BasicComboBoxRenderer extends JLabel
  29. implements ListCellRenderer, Serializable {
  30. protected static Border noFocusBorder = new EmptyBorder(1, 1, 1, 1);
  31. public BasicComboBoxRenderer() {
  32. super();
  33. setOpaque(true);
  34. setBorder(noFocusBorder);
  35. }
  36. public Dimension getPreferredSize() {
  37. Dimension size;
  38. if ((this.getText() == null) || (this.getText().equals( "" ))) {
  39. setText( " " );
  40. size = super.getPreferredSize();
  41. setText( "" );
  42. }
  43. else {
  44. size = super.getPreferredSize();
  45. }
  46. return size;
  47. }
  48. public Component getListCellRendererComponent(
  49. JList list,
  50. Object value,
  51. int index,
  52. boolean isSelected,
  53. boolean cellHasFocus)
  54. {
  55. /**if (isSelected) {
  56. setBackground(UIManager.getColor("ComboBox.selectionBackground"));
  57. setForeground(UIManager.getColor("ComboBox.selectionForeground"));
  58. } else {
  59. setBackground(UIManager.getColor("ComboBox.background"));
  60. setForeground(UIManager.getColor("ComboBox.foreground"));
  61. }**/
  62. if (isSelected) {
  63. setBackground(list.getSelectionBackground());
  64. setForeground(list.getSelectionForeground());
  65. }
  66. else {
  67. setBackground(list.getBackground());
  68. setForeground(list.getForeground());
  69. }
  70. setFont(list.getFont());
  71. if (value instanceof Icon) {
  72. setIcon((Icon)value);
  73. }
  74. else {
  75. setText((value == null) ? "" : value.toString());
  76. }
  77. return this;
  78. }
  79. /**
  80. * A subclass of BasicComboBoxRenderer that implements UIResource.
  81. * BasicComboBoxRenderer doesn't implement UIResource
  82. * directly so that applications can safely override the
  83. * cellRenderer property with BasicListCellRenderer subclasses.
  84. * <p>
  85. * <strong>Warning:</strong>
  86. * Serialized objects of this class will not be compatible with
  87. * future Swing releases. The current serialization support is
  88. * appropriate for short term storage or RMI between applications running
  89. * the same version of Swing. As of 1.4, support for long term storage
  90. * of all JavaBeans<sup><font size="-2">TM</font></sup>
  91. * has been added to the <code>java.beans</code> package.
  92. * Please see {@link java.beans.XMLEncoder}.
  93. */
  94. public static class UIResource extends BasicComboBoxRenderer implements javax.swing.plaf.UIResource {
  95. }
  96. }