小萨
magicsa
全部博文(13)
2008年(13)
greenet
庸人自扰
zhangjie
haise198
bbsworks
分类: 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 ;
程序截图如下:
姓名列:
上一篇:jface中的表格操作.添加控件(续2)
下一篇:opencms添加新页面步骤
登录 注册