kooyee ‘s blog

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

          日歷

          <2025年6月>
          25262728293031
          1234567
          891011121314
          15161718192021
          22232425262728
          293012345

          公告

          一起暢游計算機的世界

          搜索

          •  

          積分與排名

          • 積分 - 163348
          • 排名 - 363

          最新評論

          [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();        
                              }

                          }
          );
          主站蜘蛛池模板: 星子县| 马鞍山市| 古田县| 沅陵县| 岐山县| 广宁县| 福建省| 会泽县| 扎赉特旗| 白朗县| 武宁县| 淮滨县| 谷城县| 河源市| 邮箱| 大理市| 黔东| 南木林县| 托里县| 山阴县| 贡嘎县| 措美县| 武穴市| 漳浦县| 桃园市| 荔浦县| 汽车| 成武县| 嘉义市| 济宁市| 赞皇县| 新田县| 银川市| 文成县| 彭州市| 万全县| 明水县| 新密市| 恩平市| 兴仁县| 六盘水市|