<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    9910

    單飛

       :: 首頁(yè) :: 聯(lián)系 :: 聚合  :: 管理

    前邊實(shí)現(xiàn)了一個(gè)表格的基本功能,但這并不夠好,能否為表格實(shí)現(xiàn)一些更好的功能呢?答案是肯定的。下邊我們來(lái)加入直接編輯的功能。
    一、要實(shí)現(xiàn)這個(gè)功能必須提供一個(gè)實(shí)現(xiàn)ICellModifier的類(lèi)。內(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è)類(lèi),下一部就是如何把它和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)編輯,讓性別這一列變成類(lèi)似checkbox的操作,讓年齡這一類(lèi)變成直接輸入
    ok,嘗試一下。
    三、問(wèn)題出現(xiàn),如果年齡的地方我們輸入一個(gè)非數(shù)字呢,所以為了安全起見(jiàn),我們加入一個(gè)驗(yàn)證器,禁止用戶(hù)輸入非數(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í)還是有些問(wèn)題的,試著輸入個(gè)0,呵呵。這里就需要你自己按照自己的實(shí)際需求來(lái)實(shí)現(xiàn)了。
    但作為demo這個(gè)的目的已經(jīng)達(dá)到了。
    source下載:http://m.tkk7.com/Files/dreamstone/jface-3.rar
    posted on 2009-08-27 14:54 單飛 閱讀(559) 評(píng)論(0)  編輯  收藏 所屬分類(lèi): Eclipse Plugin
    主站蜘蛛池模板: 四虎免费久久影院| 全免费a级毛片免费看不卡| 亚洲一区免费视频| 成年女人喷潮毛片免费播放| 国产自产拍精品视频免费看| 亚洲精品专区在线观看| 亚洲国产第一站精品蜜芽| 18亚洲男同志videos网站| 亚洲中文字幕乱码AV波多JI| 免费的黄色的网站| 久久综合国产乱子伦精品免费 | 免费在线观看亚洲| 成人一区二区免费视频| 69精品免费视频| 日本不卡免费新一二三区| 亚洲色欲久久久综合网| 亚洲毛片基地日韩毛片基地| 亚洲av无码专区国产不乱码 | 人妻丰满熟妇无码区免费| 成年轻人网站色免费看| 国产亚洲欧洲Aⅴ综合一区| 亚洲精品美女在线观看| 西西人体大胆免费视频| 99视频精品全部免费观看| 国产高清在线免费视频| 久久精品国产亚洲香蕉| 久久精品国产亚洲AV天海翼| 日本中文字幕免费高清视频| 午夜毛片不卡免费观看视频| 人人狠狠综合久久亚洲88| 亚洲色偷偷偷综合网| 久久国产精品萌白酱免费| 国产色婷婷精品免费视频| 久久久久亚洲AV无码网站| 深夜a级毛片免费无码| 精品免费久久久久久久| 久久久无码精品亚洲日韩软件| 亚洲一区二区三区在线| 精品国产污污免费网站入口在线| 成年在线观看网站免费| 亚洲成色www久久网站夜月|