1,在使用TableViewer时,要实现隔列换色是比较容易的,只要在标签提供器里加上表格的颜色提供器的实现就可以,代码也很简单.如下:
public class XXXXLableProvider implements ITableLabelProvider, ITableColorProvider {
private Color[] bg = new Color[]{new Color(null, 255,255,255), new Color(null, 247,247,240)};
private Color[] force = new Color[]{new Color(null, 0,0,0), new Color(null, 0,0,0)};
.....
public Color getForeground(Object element, int columnIndex) {
return force[columnIndex%2];
}
public Color getBackground(Object element, int columnIndex) {
return bg[columnIndex%2];
}
}
bg是背景色,分两种,force是前景色,也是两种,分别对应,想换成其它的颜色,修改两个定义部分就可以了.
2,但要实现隔行换色就比较麻烦些了,不过还是可以实现,实现原理也很简单,就是记录上一次的对象,与本次对象如果不同就换颜色,否则一直使用当前颜色.代码如下:
public class XXXXLableProvider implements ITableLabelProvider, ITableColorProvider {
private Color[] bg = new Color[]{new Color(null, 255,255,255), new Color(null, 247,247,240)};
private Color[] force = new Color[]{new Color(null, 0,0,0), new Color(null, 0,0,0)};
private Object current = null;
private int currentColor = 0;
......
public Color getForeground(Object element, int columnIndex) {
return force[currentColor];
}
public Color getBackground(Object element, int columnIndex) {
if (current != element) {
currentColor = 1 - currentColor;
current = element;
}
return bg[currentColor];
}
}
颜色也是和上面一样,不过这样做出来的隔行换色毕竟还不是SWT表格本身支持的,如果表格行没有充满,在后面看到的还是表格的背景色(默认白色)
客户虐我千百遍,我待客户如初恋!