Get busy living or get busy dying!

            BlogJava :: 首頁 :: 新隨筆 :: 聯系 :: 聚合  :: 管理 ::
            1 隨筆 :: 13 文章 :: 0 評論 :: 0 Trackbacks
          一提到表格,人們就想得到EXCEL。不錯,這個優秀的軟件一共提供了幾千個功能點,但人們平常一般只用到其常用的幾十個功能。
          SWT/JFACE提供的表格雖然不能完成EXCEL的所有功能,但其常用的功能已經具備。比如:雙擊編輯表格、操作單元格、在單元格中加入控件。
          其關鍵就在于CellEditor的使用!
          for (int i=0; i<items.length; i++{
                  TableEditor editor 
          = new TableEditor (table);
                  CCombo combo 
          = new CCombo (table, SWT.NONE);
                  combo.setText(
          "CCombo");
                  combo.add(
          "item 1");
                  combo.add(
          "item 2");
                  editor.grabHorizontal 
          = true;
                  editor.setEditor(combo, items[i], 
          0);
                  editor 
          = new TableEditor (table);
                  Text text 
          = new Text (table, SWT.NONE);
                  text.setText(
          "Text");
                  editor.grabHorizontal 
          = true;
                  editor.setEditor(text, items[i], 
          1);
                  editor 
          = new TableEditor (table);
                  Button button 
          = new Button (table, SWT.CHECK);
                  button.pack ();
                  editor.minimumWidth 
          = button.getSize ().x;
                  editor.horizontalAlignment 
          = SWT.LEFT;
                  editor.setEditor (button, items[i], 
          2);
              }
          用以上代碼加入CCombo和Check。
          關鍵在于:
          editor.setEditor(combo, items[i], 0);
          combo為控件對象,item[i]和0為確定editor(單元格)的位置。
          有了這個函數就可以任意加控件了。特殊情況加入text控件就可實現雙擊編輯,代碼如下:
          table.addSelectionListener(new SelectionAdapter() {
                  
          public void widgetSelected(SelectionEvent e) {
                      
          // Clean up any previous editor control
                      Control oldEditor = editor.getEditor();
                      
          if (oldEditor != null) oldEditor.dispose();
              
                      
          // Identify the selected row
                      TableItem item = (TableItem)e.item;
                      
          if (item == nullreturn;
              
                      
          // The control that will be the editor must be a child of the Table
                      Text newEditor = new Text(table, SWT.NONE);
                      newEditor.setText(item.getText(EDITABLECOLUMN));
                      newEditor.addModifyListener(
          new ModifyListener() {
                          
          public void modifyText(ModifyEvent me) {
                              Text text 
          = (Text)editor.getEditor();
                              editor.getItem().setText(EDITABLECOLUMN, text.getText());
                          }

                      }
          );
                      newEditor.selectAll();
                      newEditor.setFocus();
                      editor.setEditor(newEditor, item, EDITABLECOLUMN);
                  }

              }
          );
          哈哈 這樣就可以了哦!

          posted on 2007-07-11 23:29 一條輝 閱讀(544) 評論(0)  編輯  收藏 所屬分類: SWT/JFACE

          只有注冊用戶登錄后才能發表評論。


          網站導航:
           
          主站蜘蛛池模板: 西和县| 磐安县| 嵊泗县| 苍梧县| 新干县| 依安县| 中卫市| 兴和县| 丘北县| 黔东| 阳曲县| 普陀区| 北宁市| 游戏| 阳城县| 安泽县| 清苑县| 章丘市| 龙海市| 前郭尔| 当雄县| 五大连池市| 青冈县| 筠连县| 汝南县| 卓资县| 海丰县| 辽中县| 中江县| 固安县| 江陵县| 河北区| 建湖县| 罗田县| 峨眉山市| 手机| 巴楚县| 体育| 汽车| 浮山县| 哈密市|