1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65
|
public class ColorRenderer {
private JTable table;
private AbstractTableModel model;
private Map colors;
private Point location;
public ColorRenderer(JTable table) {
this.table = table;
model = (AbstractTableModel) table.getModel();
colors = new HashMap();
location = new Point();
}
public void setBackground(Component c, int row, int column) {
if (table.isCellSelected(row, column))
return;
if (c instanceof DefaultTableCellRenderer) {
c.setBackground(table.getBackground());
}
column = table.convertColumnIndexToModel(column);
Object key = getKey(row, column);
Object o = colors.get(key);
if (o != null) {
c.setBackground((Color) o);
return;
}
key = getKey(row, -1);
o = colors.get(key);
if (o != null) {
c.setBackground((Color) o);
return;
}
key = getKey(-1, column);
o = colors.get(key);
if (o != null) {
c.setBackground((Color) o);
return;
}
}
public void setCellColor(int row, int column, Color color) {
Point key = new Point(row, column);
colors.put(key, color);
}
public void setColumnColor(int column, Color color) {
setCellColor(-1, column, color);
}
public void setRowColor(int row, Color color) {
setCellColor(row, -1, color);
}
private Object getKey(int row, int column) {
location.x = row;
location.y = column;
return location;
}
} |
Partager