大夢想家

          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 阿南

          又被捕了~
            回復  更多評論    
          主站蜘蛛池模板: 金门县| 盱眙县| 保康县| 铁岭市| 棋牌| 耒阳市| 唐山市| 西峡县| 正安县| 竹北市| 西乌珠穆沁旗| 措美县| 平陆县| 祁连县| 甘泉县| 聂拉木县| 八宿县| 浦县| 洞口县| 丰县| 斗六市| 武定县| 青浦区| 浠水县| 克什克腾旗| 石林| 自治县| 南通市| 西城区| 临江市| 高州市| 涞水县| 福鼎市| 通江县| 博客| 新闻| 肇州县| 承德市| 香河县| 辽宁省| 曲沃县|