实现SWT(JFace)里的表格隔行换色功能

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表格本身支持的,如果表格行没有充满,在后面看到的还是表格的背景色(默认白色)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值