src/share/classes/javax/swing/plaf/basic/BasicComboBoxRenderer.java

Print this page




  32 
  33 import java.io.Serializable;
  34 
  35 
  36 /**
  37  * ComboBox renderer
  38  * <p>
  39  * <strong>Warning:</strong>
  40  * Serialized objects of this class will not be compatible with
  41  * future Swing releases. The current serialization support is
  42  * appropriate for short term storage or RMI between applications running
  43  * the same version of Swing.  As of 1.4, support for long term storage
  44  * of all JavaBeans&trade;
  45  * has been added to the <code>java.beans</code> package.
  46  * Please see {@link java.beans.XMLEncoder}.
  47  *
  48  * @author Arnaud Weber
  49  */
  50 @SuppressWarnings("serial") // Same-version serialization only
  51 public class BasicComboBoxRenderer extends JLabel
  52 implements ListCellRenderer, Serializable {
  53 
  54    /**
  55     * An empty <code>Border</code>. This field might not be used. To change the
  56     * <code>Border</code> used by this renderer directly set it using
  57     * the <code>setBorder</code> method.
  58     */
  59     protected static Border noFocusBorder = new EmptyBorder(1, 1, 1, 1);
  60     private final static Border SAFE_NO_FOCUS_BORDER = new EmptyBorder(1, 1, 1, 1);
  61 
  62     public BasicComboBoxRenderer() {
  63         super();
  64         setOpaque(true);
  65         setBorder(getNoFocusBorder());
  66     }
  67 
  68     private static Border getNoFocusBorder() {
  69         if (System.getSecurityManager() != null) {
  70             return SAFE_NO_FOCUS_BORDER;
  71         } else {
  72             return noFocusBorder;
  73         }
  74     }
  75 
  76     public Dimension getPreferredSize() {
  77         Dimension size;
  78 
  79         if ((this.getText() == null) || (this.getText().equals( "" ))) {
  80             setText( " " );
  81             size = super.getPreferredSize();
  82             setText( "" );
  83         }
  84         else {
  85             size = super.getPreferredSize();
  86         }
  87 
  88         return size;
  89     }
  90 
  91     public Component getListCellRendererComponent(
  92                                                  JList list,
  93                                                  Object value,
  94                                                  int index,
  95                                                  boolean isSelected,
  96                                                  boolean cellHasFocus)
  97     {
  98 
  99         /**if (isSelected) {
 100             setBackground(UIManager.getColor("ComboBox.selectionBackground"));
 101             setForeground(UIManager.getColor("ComboBox.selectionForeground"));
 102         } else {
 103             setBackground(UIManager.getColor("ComboBox.background"));
 104             setForeground(UIManager.getColor("ComboBox.foreground"));
 105         }**/
 106 
 107         if (isSelected) {
 108             setBackground(list.getSelectionBackground());
 109             setForeground(list.getSelectionForeground());
 110         }
 111         else {
 112             setBackground(list.getBackground());




  32 
  33 import java.io.Serializable;
  34 
  35 
  36 /**
  37  * ComboBox renderer
  38  * <p>
  39  * <strong>Warning:</strong>
  40  * Serialized objects of this class will not be compatible with
  41  * future Swing releases. The current serialization support is
  42  * appropriate for short term storage or RMI between applications running
  43  * the same version of Swing.  As of 1.4, support for long term storage
  44  * of all JavaBeans&trade;
  45  * has been added to the <code>java.beans</code> package.
  46  * Please see {@link java.beans.XMLEncoder}.
  47  *
  48  * @author Arnaud Weber
  49  */
  50 @SuppressWarnings("serial") // Same-version serialization only
  51 public class BasicComboBoxRenderer extends JLabel
  52 implements ListCellRenderer<Object>, Serializable {
  53 
  54    /**
  55     * An empty <code>Border</code>. This field might not be used. To change the
  56     * <code>Border</code> used by this renderer directly set it using
  57     * the <code>setBorder</code> method.
  58     */
  59     protected static Border noFocusBorder = new EmptyBorder(1, 1, 1, 1);
  60     private final static Border SAFE_NO_FOCUS_BORDER = new EmptyBorder(1, 1, 1, 1);
  61 
  62     public BasicComboBoxRenderer() {
  63         super();
  64         setOpaque(true);
  65         setBorder(getNoFocusBorder());
  66     }
  67 
  68     private static Border getNoFocusBorder() {
  69         if (System.getSecurityManager() != null) {
  70             return SAFE_NO_FOCUS_BORDER;
  71         } else {
  72             return noFocusBorder;
  73         }
  74     }
  75 
  76     public Dimension getPreferredSize() {
  77         Dimension size;
  78 
  79         if ((this.getText() == null) || (this.getText().equals( "" ))) {
  80             setText( " " );
  81             size = super.getPreferredSize();
  82             setText( "" );
  83         }
  84         else {
  85             size = super.getPreferredSize();
  86         }
  87 
  88         return size;
  89     }
  90 
  91     public Component getListCellRendererComponent(
  92                                                  JList<? extends Object> list,
  93                                                  Object value,
  94                                                  int index,
  95                                                  boolean isSelected,
  96                                                  boolean cellHasFocus)
  97     {
  98 
  99         /**if (isSelected) {
 100             setBackground(UIManager.getColor("ComboBox.selectionBackground"));
 101             setForeground(UIManager.getColor("ComboBox.selectionForeground"));
 102         } else {
 103             setBackground(UIManager.getColor("ComboBox.background"));
 104             setForeground(UIManager.getColor("ComboBox.foreground"));
 105         }**/
 106 
 107         if (isSelected) {
 108             setBackground(list.getSelectionBackground());
 109             setForeground(list.getSelectionForeground());
 110         }
 111         else {
 112             setBackground(list.getBackground());