1 7 8 package org.gjt.jclasslib.util; 9 10 import javax.swing.*; 11 import javax.swing.border.Border ; 12 import javax.swing.border.EmptyBorder ; 13 import javax.swing.table.TableCellRenderer ; 14 import java.awt.*; 15 16 17 24 public class ExtendedTableCellRenderer extends ExtendedJLabel 25 implements TableCellRenderer { 26 27 private static final Border NO_FOCUS_BORDER = new EmptyBorder (1, 1, 1, 1); 28 29 private Color unselectedForeground; 30 private Color unselectedBackground; 31 32 35 public ExtendedTableCellRenderer() { 36 super(); 37 setOpaque(true); 38 setBorder(NO_FOCUS_BORDER); 39 } 40 41 public void setForeground(Color c) { 42 super.setForeground(c); 43 unselectedForeground = c; 44 } 45 46 public void setBackground(Color c) { 47 super.setBackground(c); 48 unselectedBackground = c; 49 } 50 51 public void updateUI() { 52 super.updateUI(); 53 setForeground(null); 54 setBackground(null); 55 } 56 57 public Component getTableCellRendererComponent(JTable table, 58 Object value, 59 boolean isSelected, 60 boolean hasFocus, 61 int row, 62 int column) 63 { 64 if (isSelected) { 65 super.setForeground(table.getSelectionForeground()); 66 super.setBackground(table.getSelectionBackground()); 67 68 } else { 69 super.setForeground((unselectedForeground != null) ? unselectedForeground : table.getForeground()); 70 super.setBackground((unselectedBackground != null) ? unselectedBackground : table.getBackground()); 71 } 72 73 setFont(table.getFont()); 74 75 if (hasFocus) { 76 setBorder(UIManager.getBorder("Table.focusCellHighlightBorder")); 77 if (table.isCellEditable(row, column)) { 78 super.setForeground(UIManager.getColor("Table.focusCellForeground")); 79 super.setBackground(UIManager.getColor("Table.focusCellBackground")); 80 } 81 } else { 82 setBorder(NO_FOCUS_BORDER); 83 } 84 85 setValue(value); 86 87 Color background = getBackground(); 88 boolean colorMatch = (background != null) && (background.equals(table.getBackground())) && table.isOpaque(); 89 setOpaque(!colorMatch); 90 91 return this; 92 } 93 94 95 public void validate() {} 96 97 public void revalidate() {} 98 99 public void repaint(long tm, int x, int y, int width, int height) {} 100 101 public void repaint(Rectangle r) { } 102 103 protected void firePropertyChange(String propertyName, Object oldValue, Object newValue) { 104 if (propertyName == "text") { 105 super.firePropertyChange(propertyName, oldValue, newValue); 106 } 107 } 108 109 public void firePropertyChange(String propertyName, boolean oldValue, boolean newValue) { } 110 111 112 private void setValue(Object value) { 113 setText((value == null) ? "" : value.toString()); 114 } 115 116 117 } 118 119 120 | Popular Tags |