大夢想家

          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 阿南 閱讀(1823) 評論(2)  編輯  收藏 所屬分類: 西安java用戶群Eclipse-SWT

          評論

          # re: {轉}實現SWT(JFace)里的表格隔行換色功能 2007-07-28 08:48 dudu

          首頁從來不允許轉載文章!
            回復  更多評論    

          # re: {轉}實現SWT(JFace)里的表格隔行換色功能 2007-07-28 09:19 阿南

          又被捕了~
            回復  更多評論    
          主站蜘蛛池模板: 基隆市| 叙永县| 平江县| 顺平县| 三河市| 申扎县| 旌德县| 玛多县| 霞浦县| 卢氏县| 蕉岭县| 竹北市| 高碑店市| 尉氏县| 崇阳县| 民和| 依安县| 文登市| 井冈山市| 邹平县| 南陵县| 莱芜市| 龙游县| 竹北市| 千阳县| 常德市| 克什克腾旗| 永安市| 聂荣县| 兴国县| 棋牌| 永宁县| 博乐市| 井陉县| 水城县| 闻喜县| 三河市| 和硕县| 高阳县| 汶川县| 潞城市|