大夢(mèng)想家

          5年開發(fā)工程師,2年實(shí)施經(jīng)理,X年售前顧問(wèn),......
          數(shù)據(jù)加載中……
          {轉(zhuǎn)}實(shí)現(xiàn)SWT(JFace)里的表格隔行換色功能
          1,在使用TableViewer時(shí),要實(shí)現(xiàn)隔列換色是比較容易的,只要在標(biāo)簽提供器里加上表格的顏色提供器的實(shí)現(xiàn)就可以,代碼也很簡(jiǎ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是前景色,也是兩種,分別對(duì)應(yīng),想換成其它的顏色,修改兩個(gè)定義部分就可以了.

          2,但要實(shí)現(xiàn)隔行換色就比較麻煩些了,不過(guò)還是可以實(shí)現(xiàn),實(shí)現(xiàn)原理也很簡(jiǎn)單,就是記錄上一次的對(duì)象,與本次對(duì)象如果不同就換顏色,否則一直使用當(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];
          ??? }
          }

          顏色也是和上面一樣,不過(guò)這樣做出來(lái)的隔行換色畢竟還不是SWT表格本身支持的,如果表格行沒(méi)有充滿,在后面看到的還是表格的背景色(默認(rèn)白色)


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

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

          評(píng)論

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

          首頁(yè)從來(lái)不允許轉(zhuǎn)載文章!

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

          又被捕了~
          主站蜘蛛池模板: 伊宁市| 景泰县| 富平县| 闸北区| 连城县| 仙桃市| 松江区| 克什克腾旗| 桑植县| 龙井市| 长宁县| 八宿县| 衡水市| 泽州县| 杨浦区| 勃利县| 连城县| 诸暨市| 昌平区| 车致| 丽江市| 织金县| 南召县| 宜春市| 朝阳县| 同仁县| 封开县| 新昌县| 祥云县| 邵武市| 弥渡县| 永康市| 定结县| 永清县| 紫金县| 额济纳旗| 监利县| 阳东县| 北流市| 博野县| 乾安县|