1 7 8 package javax.swing.table; 9 10 import javax.swing.*; 11 import javax.swing.table.TableCellRenderer ; 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 68 public class DefaultTableCellRenderer extends JLabel 69 implements TableCellRenderer , Serializable 70 { 71 72 protected static Border noFocusBorder = new EmptyBorder(1, 1, 1, 1); 73 private static final Border SAFE_NO_FOCUS_BORDER = new EmptyBorder(1, 1, 1, 1); 74 75 private Color unselectedForeground; 80 private Color unselectedBackground; 81 82 85 public DefaultTableCellRenderer() { 86 super(); 87 setOpaque(true); 88 setBorder(getNoFocusBorder()); 89 } 90 91 private static Border getNoFocusBorder() { 92 if (System.getSecurityManager() != null) { 93 return SAFE_NO_FOCUS_BORDER; 94 } else { 95 return noFocusBorder; 96 } 97 } 98 99 105 public void setForeground(Color c) { 106 super.setForeground(c); 107 unselectedForeground = c; 108 } 109 110 116 public void setBackground(Color c) { 117 super.setBackground(c); 118 unselectedBackground = c; 119 } 120 121 129 public void updateUI() { 130 super.updateUI(); 131 setForeground(null); 132 setBackground(null); 133 } 134 135 149 public Component getTableCellRendererComponent(JTable table, Object value, 150 boolean isSelected, boolean hasFocus, int row, int column) { 151 152 if (isSelected) { 153 super.setForeground(table.getSelectionForeground()); 154 super.setBackground(table.getSelectionBackground()); 155 } 156 else { 157 super.setForeground((unselectedForeground != null) ? unselectedForeground 158 : table.getForeground()); 159 super.setBackground((unselectedBackground != null) ? unselectedBackground 160 : table.getBackground()); 161 } 162 163 setFont(table.getFont()); 164 165 if (hasFocus) { 166 Border border = null; 167 if (isSelected) { 168 border = UIManager.getBorder("Table.focusSelectedCellHighlightBorder"); 169 } 170 if (border == null) { 171 border = UIManager.getBorder("Table.focusCellHighlightBorder"); 172 } 173 setBorder(border); 174 175 if (!isSelected && table.isCellEditable(row, column)) { 176 Color col; 177 col = UIManager.getColor("Table.focusCellForeground"); 178 if (col != null) { 179 super.setForeground(col); 180 } 181 col = UIManager.getColor("Table.focusCellBackground"); 182 if (col != null) { 183 super.setBackground(col); 184 } 185 } 186 } else { 187 setBorder(getNoFocusBorder()); 188 } 189 190 setValue(value); 191 192 return this; 193 } 194 195 202 203 208 public boolean isOpaque() { 209 Color back = getBackground(); 210 Component p = getParent(); 211 if (p != null) { 212 p = p.getParent(); 213 } 214 boolean colorMatch = (back != null) && (p != null) && 216 back.equals(p.getBackground()) && 217 p.isOpaque(); 218 return !colorMatch && super.isOpaque(); 219 } 220 221 228 public void invalidate() {} 229 230 235 public void validate() {} 236 237 242 public void revalidate() {} 243 244 249 public void repaint(long tm, int x, int y, int width, int height) {} 250 251 256 public void repaint(Rectangle r) { } 257 258 265 public void repaint() { 266 } 267 268 273 protected void firePropertyChange(String propertyName, Object oldValue, Object newValue) { 274 if (propertyName=="text") { 276 super.firePropertyChange(propertyName, oldValue, newValue); 277 } 278 } 279 280 285 public void firePropertyChange(String propertyName, boolean oldValue, boolean newValue) { } 286 287 288 297 protected void setValue(Object value) { 298 setText((value == null) ? "" : value.toString()); 299 } 300 301 302 320 public static class UIResource extends DefaultTableCellRenderer 321 implements javax.swing.plaf.UIResource 322 { 323 } 324 325 } 326 327 328 | Popular Tags |