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

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

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

    DANCE WITH JAVA

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

    常用鏈接

    統(tǒng)計(jì)

    積分與排名

    好友之家

    最新評(píng)論

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

    前邊實(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 2007-08-05 13:21 dreamstone 閱讀(7597) 評(píng)論(6)  編輯  收藏 所屬分類(lèi): SWT和插件開(kāi)發(fā)

    評(píng)論

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

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

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

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

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

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

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

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

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

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

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

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

    主站蜘蛛池模板: 亚洲精品无码人妻无码| 久久久久亚洲AV片无码| 青娱乐免费在线视频| 男女一进一出抽搐免费视频| 亚洲日韩一区二区三区| 亚洲图片中文字幕| 免费黄色小视频网站| 国产成人精品高清免费| 在线亚洲精品自拍| 国产免费av片在线播放| 免费做爰猛烈吃奶摸视频在线观看| 久久成人免费电影| 自拍日韩亚洲一区在线| 亚洲最大福利视频网站| 69成人免费视频无码专区| 91禁漫免费进入| 老司机亚洲精品影院在线观看| 中文字幕亚洲综合小综合在线| 亚洲视频免费在线播放| 亚洲精品国产精品乱码不卡√| 中文字幕亚洲专区| 亚洲精品和日本精品| 青青青国产手机频在线免费观看 | 99re在线精品视频免费| 国产午夜成人免费看片无遮挡 | 久久久久国产精品免费免费搜索| 最近免费字幕中文大全视频| 日本xxxx色视频在线观看免费| 97人妻精品全国免费视频| a级毛片免费播放| 亚洲熟女www一区二区三区| 国产色在线|亚洲| 亚洲乱亚洲乱妇24p| 亚洲人片在线观看天堂无码| 亚洲另类无码专区首页| 亚洲AV无码XXX麻豆艾秋| 精品女同一区二区三区免费播放| 狼色精品人妻在线视频免费| 亚洲另类自拍丝袜第1页| 在线观看亚洲天天一三视| 久久精品国产69国产精品亚洲|