DANCE WITH JAVA

          開發(fā)出高質量的系統(tǒng)

          常用鏈接

          統(tǒng)計

          積分與排名

          好友之家

          最新評論

          Swt/Jface tableViewer入門教程三(加入在表格上直接編輯數(shù)據(jù))

          前邊實現(xiàn)了一個表格的基本功能,但這并不夠好,能否為表格實現(xiàn)一些更好的功能呢?答案是肯定的。下邊我們來加入直接編輯的功能。
          一、要實現(xiàn)這個功能必須提供一個實現(xiàn)ICellModifier的類。內容如下

          import org.eclipse.jface.viewers.ICellModifier;
          import org.eclipse.jface.viewers.TableViewer;
          import org.eclipse.swt.widgets.TableItem;

          public class MyCellModifier implements ICellModifier{
                  
          private TableViewer tv;
                  
          public static String[] NAMES ={"張三","李四","小紅","翠花"};
                  
                  
          public MyCellModifier(TableViewer tv){
                          
          this.tv = tv;
                  }

                  
          public boolean canModify(Object element, String property) {
                      
          return true;
                  }


                  
          public Object getValue(Object element, String property) {
                      People p 
          = (People)element;
                      
          if(property.equals("name")){
                          
          return new Integer(getNameIndex(p.getName()));
                      }
          else if(property.equals("sex")){
                          
          return new Boolean(p.getSex().equals(""));
                      }
          else if(property.equals("age")){
                          
          return String.valueOf(p.getAge());
                      }

                      
          throw new RuntimeException("error column name : " + property);
                  }

                  
          private int getNameIndex(String name){
                      
          for(int i=0;i<NAMES.length;i++){
                          
          if(NAMES[i].equals(name)){
                              
          return i;
                          }

                      }

                      
          return -1;
                  }


                  
          public void modify(Object element, String property, Object value) {
                      TableItem item 
          = (TableItem)element;
                      People p 
          = (People)item.getData();
                      
          if (property.equals("name")){
                          Integer comboIndex 
          = (Integer)value;
                          
          if(comboIndex.intValue() == -1){
                              
          return ;
                          }

                          String newName 
          = NAMES[comboIndex.intValue()];
                          p.setName(newName);
                      }
          else if(property.equals("sex")){
                          Boolean newValue 
          = (Boolean)value;
                          System.out.println(newValue);
                          
          if(newValue.booleanValue()){
                              p.setSex(
          "");
                          }
          else{
                              p.setSex(
          "");
                          }

                      }
          else if (property.equals("age")){
                          String newValue 
          = (String)value;
                          
          if(newValue.equals("")){
                              
          return ;
                          }

                          Integer newAge 
          = new Integer(newValue);
                          p.setAge(newAge);
                      }
          else{
                          
          throw new RuntimeException("錯誤列名:" + property);
                      }

                      tv.update(p, 
          null);
                  }

                  
              }

          二、好了,有了這個類,下一部就是如何把它和TestTableViewer關聯(lián)起來,在TestTableViewer中setInput()后加入如下內容
          tableViewer.setColumnProperties(new String[]{"id","name","sex","age","createDate"});
                  CellEditor[] cellEditor 
          = new CellEditor[5];
                  cellEditor[
          0= null;
                  cellEditor[
          1= new ComboBoxCellEditor(tableViewer.getTable(),MyCellModifier.NAMES,SWT.READ_ONLY);
                  cellEditor[
          2= new CheckboxCellEditor(tableViewer.getTable());
                  cellEditor[
          3= new TextCellEditor(tableViewer.getTable());
                  cellEditor[
          4= null;
                  tableViewer.setCellEditors(cellEditor);
                  ICellModifier modifier 
          = new MyCellModifier(tableViewer);
                  tableViewer.setCellModifier(modifier);
          我們讓名字這一列用下拉條來編輯,讓性別這一列變成類似checkbox的操作,讓年齡這一類變成直接輸入
          ok,嘗試一下。
          三、問題出現(xiàn),如果年齡的地方我們輸入一個非數(shù)字呢,所以為了安全起見,我們加入一個驗證器,禁止用戶輸入非數(shù)字
          在上邊的內容下加入
          Text text = (Text)cellEditor[3].getControl();
                  text.addVerifyListener(
          new VerifyListener(){
                      
          public void verifyText(VerifyEvent e){
                          String inStr 
          = e.text;
                          
          if (inStr.length() > 0){
                              
          try{
                                  Integer.parseInt(inStr);
                                  e.doit 
          = true;
                              }
          catch(Exception ep){
                                  e.doit 
          = false;
                              }

                          }

                      }

                  }
          );
          好了,再試試是否不能輸入非整數(shù)了?解決。其實還是有些問題的,試著輸入個0,呵呵。這里就需要你自己按照自己的實際需求來實現(xiàn)了。
          但作為demo這個的目的已經達到了。
          source下載:http://www.aygfsteel.com/Files/dreamstone/jface-3.rar

          posted on 2007-08-05 13:21 dreamstone 閱讀(7592) 評論(6)  編輯  收藏 所屬分類: SWT和插件開發(fā)

          評論

          # re: Swt/Jface tableViewer入門教程三(加入在表格上直接編輯數(shù)據(jù)) 2007-08-05 16:32 BeanSoft

          支持一下!  回復  更多評論   

          # re: Swt/Jface tableViewer入門教程三(加入在表格上直接編輯數(shù)據(jù)) 2008-05-31 15:15 xiaosa

          請假下lz,這樣添加cellEditor是一整列都添加的,我想指定某一個單元格添加,該如何實現(xiàn)呢?  回復  更多評論   

          # re: Swt/Jface tableViewer入門教程三(加入在表格上直接編輯數(shù)據(jù)) 2008-06-26 18:00 qwe

          good  回復  更多評論   

          # re: Swt/Jface tableViewer入門教程三(加入在表格上直接編輯數(shù)據(jù)) 2008-07-30 10:38 wangning

          寫的太好了,幫了大忙了
          謝謝  回復  更多評論   

          # re: Swt/Jface tableViewer入門教程三(加入在表格上直接編輯數(shù)據(jù)) 2012-05-02 11:39 LFZ

          謝謝!  回復  更多評論   

          # re: Swt/Jface tableViewer入門教程三(加入在表格上直接編輯數(shù)據(jù))[未登錄] 2013-07-16 22:04 匿名

          無私啊,樓主,非常感謝,有空再謝謝博客唄 ^_^  回復  更多評論   

          主站蜘蛛池模板: 满洲里市| 卢氏县| 阿拉善左旗| 锦州市| 凤凰县| 通山县| 上犹县| 汉阴县| 榆社县| 舞钢市| 巴彦淖尔市| 大新县| 马公市| 岳西县| 阿城市| 陆河县| 深水埗区| 准格尔旗| 平阴县| 绥芬河市| 武夷山市| 高碑店市| 葫芦岛市| 湖州市| 黑河市| 鄂托克前旗| 嘉兴市| 建平县| 平山县| 华蓥市| 锡林浩特市| 澎湖县| 贵阳市| 金山区| 攀枝花市| 霍邱县| 怀化市| 汉阴县| 财经| 尤溪县| 泸溪县|