kooyee ‘s blog

          開源軟件, 眾人努力的結(jié)晶, 全人類的共同財富
          posts - 103, comments - 55, trackbacks - 0, articles - 66
             :: 首頁 :: 新隨筆 :: 聯(lián)系 :: 聚合  :: 管理

          [SWT]不用jface給表格的單元格增加編輯功能

          Posted on 2007-07-20 21:14 kooyee 閱讀(842) 評論(0)  編輯  收藏 所屬分類: GUI骨衣

          先建立table editor 

          //create table editor 
             final TableEditor editor = new TableEditor(tablePrice);
             
          //The editor must have the same size as the cell and must
             
          //not be any smaller than 50 pixels.
             editor.horizontalAlignment = SWT.LEFT;
             editor.grabHorizontal 
          = true;
             editor.minimumWidth 
          = 50;

           

          下面的Listener 用來確定表格中鼠標選定的單元格的序號 

          //   listerner for the cell that mouse point to
             tablePrice.addListener(SWT.MouseDown, new Listener() {
              
          public void handleEvent(Event event{
               Point pt 
          = new Point(event.x, event.y);
               TableItem item 
          = tablePrice.getItem(pt);
               
          if (item == null)
                
          return;
               
          for (int i = 0; i < 5; i++{
                Rectangle rect 
          = item.getBounds(i);
                
          if (rect.contains(pt)) {
                 EDITABLECOLUMN 
          = i;
                }

               }

              }

             }
          );

           

          加入這個Listener對選定的項進行編輯

          tablePrice.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(tablePrice, SWT.NONE);
               newEditor.setText(item.getText(EDITABLECOLUMN));
          //取得當前單元格里的值
                newEditor.addModifyListener(new ModifyListener() //text Modify 的Listener
                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);
              }

             }
          );

           如果需要在table更新時text editor消失,可以加入以下code

          item.addDisposeListener(new org.eclipse.swt.events.DisposeListener(){
                              
          public void widgetDisposed(org.eclipse.swt.events.DisposeEvent e) {
                                  
          //editor.dispose();
                                  newEditor.dispose();
                                  
                              }

                          }
          );

           

          或者當非激活時,除去text editor

          newEditor.addFocusListener(new org.eclipse.swt.events.FocusAdapter() {
                              
          public void focusLost(org.eclipse.swt.events.FocusEvent e) {
                                  newEditor.dispose();        
                              }

                          }
          );
          主站蜘蛛池模板: 嘉定区| 满城县| 密云县| 枞阳县| 庆云县| 通河县| 苍溪县| 瑞安市| 贵阳市| 灌云县| 阳东县| 江油市| 石景山区| 封丘县| 安阳市| 烟台市| 长汀县| 新安县| 江都市| 浦东新区| 蒙城县| 阿拉善盟| 宿州市| 微博| 恭城| 壤塘县| 西充县| 淮阳县| 高碑店市| 酒泉市| 余江县| 错那县| 龙川县| 玛纳斯县| 民县| 陆丰市| 正镶白旗| 当阳市| 湖南省| 涿州市| 万安县|