1 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 ; 16 17 18 33 public class BasicComboBoxRenderer extends JLabel 34 implements ListCellRenderer, Serializable { 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 value, 71 int index, 72 boolean isSelected, 73 boolean cellHasFocus) 74 { 75 76 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 120 public static class UIResource extends BasicComboBoxRenderer implements javax.swing.plaf.UIResource { 121 } 122 } 123 124 125 | Popular Tags |