Get busy living or get busy dying!

            BlogJava :: 首頁 :: 新隨筆 :: 聯(lián)系 :: 聚合  :: 管理 ::
            1 隨筆 :: 13 文章 :: 0 評(píng)論 :: 0 Trackbacks
          一提到表格,人們就想得到EXCEL。不錯(cuò),這個(gè)優(yōu)秀的軟件一共提供了幾千個(gè)功能點(diǎn),但人們平常一般只用到其常用的幾十個(gè)功能。
          SWT/JFACE提供的表格雖然不能完成EXCEL的所有功能,但其常用的功能已經(jīng)具備。比如:雙擊編輯表格、操作單元格、在單元格中加入控件。
          其關(guān)鍵就在于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。
          關(guān)鍵在于:
          editor.setEditor(combo, items[i], 0);
          combo為控件對(duì)象,item[i]和0為確定editor(單元格)的位置。
          有了這個(gè)函數(shù)就可以任意加控件了。特殊情況加入text控件就可實(shí)現(xiàn)雙擊編輯,代碼如下:
          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 一條輝 閱讀(543) 評(píng)論(0)  編輯  收藏 所屬分類: SWT/JFACE

          只有注冊(cè)用戶登錄后才能發(fā)表評(píng)論。


          網(wǎng)站導(dǎo)航:
           
          主站蜘蛛池模板: 横峰县| 吉安县| 南澳县| 陵川县| 蛟河市| 资阳市| 黔西县| 嘉黎县| 新乡县| 勐海县| 石屏县| 澜沧| 云林县| 玉屏| 乌鲁木齐市| 巴彦淖尔市| 沂南县| 晋江市| 铜山县| 红桥区| 安西县| 耒阳市| 平顶山市| 郓城县| 招远市| 呼图壁县| 那曲县| 苏州市| 射洪县| 依兰县| 雅安市| 庆城县| 双鸭山市| 尖扎县| 思茅市| 长武县| 卢龙县| 龙川县| 黄平县| 高碑店市| 大丰市|