1 7 8 package javax.swing; 9 10 import javax.swing.*; 11 import javax.swing.event.*; 12 import javax.swing.border.*; 13 14 import java.awt.Component ; 15 import java.awt.Color ; 16 import java.awt.Rectangle ; 17 18 import java.io.Serializable ; 19 20 21 55 public class DefaultListCellRenderer extends JLabel 56 implements ListCellRenderer , Serializable 57 { 58 59 protected static Border noFocusBorder = new EmptyBorder(1, 1, 1, 1); 60 private static final Border SAFE_NO_FOCUS_BORDER = new EmptyBorder(1, 1, 1, 1); 61 62 66 public DefaultListCellRenderer() { 67 super(); 68 setOpaque(true); 69 setBorder(getNoFocusBorder()); 70 } 71 72 73 private static Border getNoFocusBorder() { 74 if (System.getSecurityManager() != null) { 75 return SAFE_NO_FOCUS_BORDER; 76 } else { 77 return noFocusBorder; 78 } 79 } 80 81 public Component getListCellRendererComponent( 82 JList list, 83 Object value, 84 int index, 85 boolean isSelected, 86 boolean cellHasFocus) 87 { 88 setComponentOrientation(list.getComponentOrientation()); 89 if (isSelected) { 90 setBackground(list.getSelectionBackground()); 91 setForeground(list.getSelectionForeground()); 92 } 93 else { 94 setBackground(list.getBackground()); 95 setForeground(list.getForeground()); 96 } 97 98 if (value instanceof Icon ) { 99 setIcon((Icon )value); 100 setText(""); 101 } 102 else { 103 setIcon(null); 104 setText((value == null) ? "" : value.toString()); 105 } 106 107 setEnabled(list.isEnabled()); 108 setFont(list.getFont()); 109 110 Border border = null; 111 if (cellHasFocus) { 112 if (isSelected) { 113 border = UIManager.getBorder("List.focusSelectedCellHighlightBorder"); 114 } 115 if (border == null) { 116 border = UIManager.getBorder("List.focusCellHighlightBorder"); 117 } 118 } else { 119 border = getNoFocusBorder(); 120 } 121 setBorder(border); 122 123 return this; 124 } 125 126 127 137 public boolean isOpaque() { 138 Color back = getBackground(); 139 Component p = getParent(); 140 if (p != null) { 141 p = p.getParent(); 142 } 143 boolean colorMatch = (back != null) && (p != null) && 145 back.equals(p.getBackground()) && 146 p.isOpaque(); 147 return !colorMatch && super.isOpaque(); 148 } 149 150 155 public void validate() {} 156 157 164 public void invalidate() {} 165 166 173 public void repaint() {} 174 175 180 public void revalidate() {} 181 186 public void repaint(long tm, int x, int y, int width, int height) {} 187 188 193 public void repaint(Rectangle r) {} 194 195 200 protected void firePropertyChange(String propertyName, Object oldValue, Object newValue) { 201 if (propertyName=="text") 203 super.firePropertyChange(propertyName, oldValue, newValue); 204 } 205 206 211 public void firePropertyChange(String propertyName, byte oldValue, byte newValue) {} 212 213 218 public void firePropertyChange(String propertyName, char oldValue, char newValue) {} 219 220 225 public void firePropertyChange(String propertyName, short oldValue, short newValue) {} 226 227 232 public void firePropertyChange(String propertyName, int oldValue, int newValue) {} 233 234 239 public void firePropertyChange(String propertyName, long oldValue, long newValue) {} 240 241 246 public void firePropertyChange(String propertyName, float oldValue, float newValue) {} 247 248 253 public void firePropertyChange(String propertyName, double oldValue, double newValue) {} 254 255 260 public void firePropertyChange(String propertyName, boolean oldValue, boolean newValue) {} 261 262 277 public static class UIResource extends DefaultListCellRenderer 278 implements javax.swing.plaf.UIResource 279 { 280 } 281 282 } 283 | Popular Tags |