Chinaunix首页 | 论坛 | 博客
  • 博客访问: 170094
  • 博文数量: 13
  • 博客积分: 2530
  • 博客等级: 少校
  • 技术积分: 485
  • 用 户 组: 普通用户
  • 注册时间: 2007-11-15 18:31
文章分类

全部博文(13)

文章存档

2008年(13)

我的朋友

分类: Java

2008-05-31 14:29:50

完整代码如下:

MyCellModifier.java

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 viewer ;
    
    public static String[] sName = new String[]{
        "帅哥0",    "帅哥1",    "帅哥2",    "帅哥3",    "帅哥4",
        "帅哥5",    "帅哥6",    "帅哥7",    "帅哥8",    "帅哥9"    } ;
    
    public static String[] SEX = new String[]{"男","女"} ;
    
    public MyCellModifier(TableViewer viewer){
        this.viewer = viewer ;
    }
    
    @Override
    public boolean canModify(Object element, String property) {
        
// TODO Auto-generated method stub

        
//设定id列不可编辑

        if(property.equals(TableConfigure.COLUMN_NAME[0])){
            return false ;
        }
        return true;
    }

    @Override
    public Object getValue(Object element, String property) {
        Person p = (Person)element ;
        
//在第一列中添加combo下拉控件,需要根据名称查找控件数组中的下标id

        if(property.equals(TableConfigure.COLUMN_NAME[1]))
            return new Integer(getNameIndex(p.getName()));
        
//性别列

        else if(property.equals(TableConfigure.COLUMN_NAME[2]))
            return new Integer(getSexIndex(p.getSex()));
        
//年龄列

        else if(property.equals(TableConfigure.COLUMN_NAME[3]))
            return String.valueOf(p.getAge());
        
        throw new RuntimeException("error column name : " + property);
        
    }

    @Override
    public void modify(Object element, String property, Object value) {
        TableItem item = (TableItem)element;
        Person p = (Person)item.getData();

        if (property.equals(TableConfigure.COLUMN_NAME[1])){
            Integer comboIndex1 = (Integer)value;
            if(comboIndex1.intValue() == -1){
                return ;
            }
            String newName = sName[comboIndex1.intValue()];
            p.setName(newName);
        }
        else if(property.equals(TableConfigure.COLUMN_NAME[2])){
            Integer comboIndex2 = (Integer)value;
            if(comboIndex2.intValue() == -1){
                return ;
            }
            String sex = SEX[comboIndex2.intValue()];
            p.setSex(sex) ;
        }
        else if(property.equals(TableConfigure.COLUMN_NAME[3])){
            String newAge = (String)value ;
            if(newAge.equals("")){
                return ;
            }
            p.setAge(Integer.parseInt(newAge)) ;
        }
        viewer.refresh() ;
    }
    
    
    private int getNameIndex(String name){
        for(int i=0;i<sName.length;i++){
            if(sName[i].equals(name)){
                return i;
            }
        }
        return -1;
    }
    private int getSexIndex(String name){
        for(int i=0;i<SEX.length;i++){
            if(SEX[i].equals(name)){
                return i;
            }
        }
        return -1;
    }
}

TableWindow.java中新增字段:

 

//添加数据集

        tableViewer.setInput(Person.getPerson()) ;
        //为了编辑单元格的需要,为表格添加列属性"myProperty",对应在MyCellModifier中的property.equals(myProperty)

        tableViewer.setColumnProperties(TableConfigure.COLUMN_NAME) ;
        //添加表格编辑

        CellEditor[] editors = new CellEditor[5] ;
        editors[0] = null ;
        editors[3] = null ;
        editors[4] = null ;
        editors[1] = new ComboBoxCellEditor(tableViewer.getTable(),
                MyCellModifier.sName,SWT.READ_ONLY) ;
        editors[2] = new ComboBoxCellEditor(tableViewer.getTable(),
                MyCellModifier.SEX,SWT.READ_ONLY) ;
        editors[3] = new TextCellEditor(tableViewer.getTable()) ;
        
        tableViewer.setCellEditors(editors) ;
        ICellModifier modifier = new MyCellModifier(tableViewer) ;
        tableViewer.setCellModifier(modifier) ;
        return parent ;

程序截图如下:

姓名列:

 
性别列:
 
年龄列:
阅读(1553) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~