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