大夢想家

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

          評論

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

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

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

          又被捕了~
            回復  更多評論    
          主站蜘蛛池模板: 松原市| 双桥区| 巴里| 从化市| 嘉善县| 道真| 灵璧县| 望谟县| 遵义市| 木里| 马尔康县| 喀什市| 肇东市| 白山市| 无锡市| 永昌县| 仁寿县| 内乡县| 都昌县| 敖汉旗| 彰化县| 大足县| 土默特左旗| 涪陵区| 屯留县| 临澧县| 深水埗区| 岑巩县| 沈阳市| 通江县| 社会| 寿光市| 绥宁县| 大理市| 新源县| 多伦县| 怀仁县| 河北区| 安西县| 广河县| 峨眉山市|