DANCE WITH JAVA

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

          常用鏈接

          統(tǒng)計(jì)

          積分與排名

          好友之家

          最新評(píng)論

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

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

          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("錯(cuò)誤列名:" + property);
                      }

                      tv.update(p, 
          null);
                  }

                  
              }

          二、好了,有了這個(gè)類,下一部就是如何把它和TestTableViewer關(guān)聯(lián)起來(lái),在TestTableViewer中setInput()后加入如下內(nèi)容
          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);
          我們讓名字這一列用下拉條來(lái)編輯,讓性別這一列變成類似checkbox的操作,讓年齡這一類變成直接輸入
          ok,嘗試一下。
          三、問題出現(xiàn),如果年齡的地方我們輸入一個(gè)非數(shù)字呢,所以為了安全起見,我們加入一個(gè)驗(yàn)證器,禁止用戶輸入非數(shù)字
          在上邊的內(nèi)容下加入
          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ù)了?解決。其實(shí)還是有些問題的,試著輸入個(gè)0,呵呵。這里就需要你自己按照自己的實(shí)際需求來(lái)實(shí)現(xiàn)了。
          但作為demo這個(gè)的目的已經(jīng)達(dá)到了。
          source下載:http://www.aygfsteel.com/Files/dreamstone/jface-3.rar

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

          評(píng)論

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

          支持一下!  回復(fù)  更多評(píng)論   

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

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

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

          good  回復(fù)  更多評(píng)論   

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

          寫的太好了,幫了大忙了
          謝謝  回復(fù)  更多評(píng)論   

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

          謝謝!  回復(fù)  更多評(píng)論   

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

          無(wú)私啊,樓主,非常感謝,有空再謝謝博客唄 ^_^  回復(fù)  更多評(píng)論   

          主站蜘蛛池模板: 德兴市| 长武县| 庐江县| 高要市| 西贡区| 三台县| 武安市| 雷山县| 洪洞县| 岳阳市| 天水市| 周口市| 盐边县| 临夏县| 乐都县| 体育| 台湾省| 奇台县| 江油市| 于都县| 鹰潭市| 墨江| 静海县| 楚雄市| 九龙县| 常州市| 五寨县| 静宁县| 衡东县| 长春市| 北碚区| 平陆县| 墨玉县| 定南县| 忻城县| 南皮县| 肇州县| 郯城县| 米易县| 曲阳县| 平湖市|