KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > javax > swing > plaf > basic > BasicComboBoxRenderer


1 /*
2  * @(#)BasicComboBoxRenderer.java 1.22 05/10/31
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
9 import javax.swing.*;
10 import javax.swing.event.*;
11 import javax.swing.border.*;
12
13 import java.awt.*;
14
15 import java.io.Serializable JavaDoc;
16
17
18 /**
19  * ComboBox renderer
20  * <p>
21  * <strong>Warning:</strong>
22  * Serialized objects of this class will not be compatible with
23  * future Swing releases. The current serialization support is
24  * appropriate for short term storage or RMI between applications running
25  * the same version of Swing. As of 1.4, support for long term storage
26  * of all JavaBeans<sup><font size="-2">TM</font></sup>
27  * has been added to the <code>java.beans</code> package.
28  * Please see {@link java.beans.XMLEncoder}.
29  *
30  * @version 1.22 10/31/05
31  * @author Arnaud Weber
32  */

33 public class BasicComboBoxRenderer extends JLabel
34 implements ListCellRenderer, Serializable JavaDoc {
35
36     protected static Border noFocusBorder = new EmptyBorder(1, 1, 1, 1);
37     private final static Border SAFE_NO_FOCUS_BORDER = new EmptyBorder(1, 1, 1, 1);
38
39     public BasicComboBoxRenderer() {
40         super();
41         setOpaque(true);
42         setBorder(getNoFocusBorder());
43     }
44     
45     private static Border getNoFocusBorder() {
46         if (System.getSecurityManager() != null) {
47             return SAFE_NO_FOCUS_BORDER;
48         } else {
49             return noFocusBorder;
50         }
51     }
52     
53     public Dimension getPreferredSize() {
54         Dimension size;
55         
56         if ((this.getText() == null) || (this.getText().equals( "" ))) {
57             setText( " " );
58             size = super.getPreferredSize();
59             setText( "" );
60         }
61         else {
62             size = super.getPreferredSize();
63         }
64         
65         return size;
66     }
67
68     public Component getListCellRendererComponent(
69                                                  JList list,
70                                                  Object JavaDoc value,
71                                                  int index,
72                                                  boolean isSelected,
73                                                  boolean cellHasFocus)
74     {
75
76         /**if (isSelected) {
77             setBackground(UIManager.getColor("ComboBox.selectionBackground"));
78             setForeground(UIManager.getColor("ComboBox.selectionForeground"));
79         } else {
80             setBackground(UIManager.getColor("ComboBox.background"));
81             setForeground(UIManager.getColor("ComboBox.foreground"));
82         }**/

83
84         if (isSelected) {
85             setBackground(list.getSelectionBackground());
86             setForeground(list.getSelectionForeground());
87         }
88         else {
89             setBackground(list.getBackground());
90             setForeground(list.getForeground());
91         }
92
93         setFont(list.getFont());
94
95         if (value instanceof Icon) {
96             setIcon((Icon)value);
97         }
98         else {
99             setText((value == null) ? "" : value.toString());
100         }
101         return this;
102     }
103
104
105     /**
106      * A subclass of BasicComboBoxRenderer that implements UIResource.
107      * BasicComboBoxRenderer doesn't implement UIResource
108      * directly so that applications can safely override the
109      * cellRenderer property with BasicListCellRenderer 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
114      * appropriate for short term storage or RMI between applications running
115      * the same version of Swing. As of 1.4, support for long term storage
116      * of all JavaBeans<sup><font size="-2">TM</font></sup>
117      * has been added to the <code>java.beans</code> package.
118      * Please see {@link java.beans.XMLEncoder}.
119      */

120     public static class UIResource extends BasicComboBoxRenderer JavaDoc implements javax.swing.plaf.UIResource JavaDoc {
121     }
122 }
123
124
125
Popular Tags