实践-全程

预测未来的最好办法,就是把它创造出来 --- 阿伦.凯
数据加载中……
{转}实现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表格本身支持的,如果表格行没有充满,在后面看到的还是表格的背景色(默认白色)


声明:  
  本BLOG内的所有文章,未经特别说明,均采用“署名-非商业性使用-禁止演绎 2.5 中国大陆”授权。任何违反本协议的行为均属于非法行为。如需非商业性转载,请保留署名。如需商业性转载出版,请直接和我联系。

Creative Commons License

posted on 2007-07-27 19:47 阿南 阅读(589) 评论(2)  编辑  收藏 所属分类: Eclipse-SWT西安java用户群

评论

# re: {转}实现SWT(JFace)里的表格隔行换色功能 2007-07-28 08:48 dudu

首页从来不允许转载文章!
  回复  更多评论    

# re: {转}实现SWT(JFace)里的表格隔行换色功能 2007-07-28 09:19 阿南

又被捕了~
  回复  更多评论    



标题  
姓名  
主页
验证码 *  
内容(请不要发表任何与政治相关的内容)  
  登录  使用高级评论  新用户注册  返回页首  恢复上次提交