大夢想家

          5年開發(fā)工程師,2年實施經(jīng)理,X年售前顧問,......
          數(shù)據(jù)加載中……
          {轉(zhuǎn)}實現(xiàn)SWT(JFace)里的表格隔行換色功能
          1,在使用TableViewer時,要實現(xiàn)隔列換色是比較容易的,只要在標(biāo)簽提供器里加上表格的顏色提供器的實現(xiàn)就可以,代碼也很簡單.如下:

          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是前景色,也是兩種,分別對應(yīng),想換成其它的顏色,修改兩個定義部分就可以了.

          2,但要實現(xiàn)隔行換色就比較麻煩些了,不過還是可以實現(xiàn),實現(xiàn)原理也很簡單,就是記錄上一次的對象,與本次對象如果不同就換顏色,否則一直使用當(dāng)前顏色.代碼如下:

          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表格本身支持的,如果表格行沒有充滿,在后面看到的還是表格的背景色(默認(rèn)白色)


          客戶虐我千百遍,我待客戶如初戀!

          posted on 2007-07-27 19:47 阿南 閱讀(1821) 評論(2)  編輯  收藏 所屬分類: 西安java用戶群Eclipse-SWT

          評論

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

          首頁從來不允許轉(zhuǎn)載文章!
            回復(fù)  更多評論    

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

          又被捕了~
            回復(fù)  更多評論    
          主站蜘蛛池模板: 固阳县| 和顺县| 海安县| 搜索| 贵南县| 高密市| 衡阳县| 饶平县| 常州市| 德保县| 德格县| 德阳市| 沁水县| 寿阳县| 开封市| 石台县| 泉州市| 平舆县| 诸城市| 滁州市| 泽州县| 苍溪县| 廉江市| 滦平县| 平武县| 绥化市| 哈密市| 岑溪市| 巴林右旗| 花莲市| 宁都县| 尖扎县| 德清县| 林周县| 广平县| 日照市| 双流县| 兴业县| 六盘水市| 鄂托克旗| 比如县|