大梦想家

5年开发工程师,2年实施经理,X年售前顾问,......
数据加载中……
{转}实现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表格本身支持的,如果表格行没有充满,在后面看到的还是表格的背景色(默认白色)


客户虐我千百遍,我待客户如初恋!

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

评论

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

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

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

又被捕了~
  回复  更多评论    

只有注册用户登录后才能发表评论。


网站导航: