欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

tableViewer在表格上直接编辑数据

程序员文章站 2022-06-17 18:26:07
...

个表格的基本功能,但这并不够好,能否为表格实现一些更好的功能呢?答案是肯定的。下边我们来加入直接编辑的功能。
一、要实现这个功能必须提供一个实现ICellModifier的类

package com.unutrip.plugin.dev.views;

import org.eclipse.jface.viewers.ICellModifier;
import org.eclipse.jface.viewers.TableViewer;
import org.eclipse.swt.widgets.TableItem;

import com.unutrip.plugin.dev.model.Person;
/**
 * 修改列的使用
 * @author longgangbai
 *
 */
public class PersonCellModifier implements ICellModifier{
         private TableViewer tv;
         public static String[] NAMES ={"张三","李四","小红","翠花"};
         
         public PersonCellModifier(TableViewer tv){
                 this.tv = tv;
         }
         public boolean canModify(Object element, String property) {
             return true;
         }
            /**
             * 获取数据时调用
             */
         public Object getValue(Object element, String property) {
             Person p = (Person)element;
             if(property.equals("stuName")){
                 return p.getStuName();
             }else if(property.equals("stuNo")){
              return p.getStuNo();
             }else if(property.equals("stuSex")){
                 return new Boolean(p.getStuSex().equals("男"));
             }else if(property.equals("stuAge")){
                 return String.valueOf(p.getStuAge());
             }else if(property.equals("stuAddr")){
                 return String.valueOf(p.getStuAddr());
             }
             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;
             Person p = (Person)item.getData();
             if (property.equals("stuName")){
                 Integer comboIndex = (Integer)value;
                 if(comboIndex.intValue() == -1){
                     return ;
                 }
                 String newName = NAMES[comboIndex.intValue()];
                 p.setStuName(newName);
             }else if(property.equals("stuSex")){
                 Boolean newValue = (Boolean)value;
                 System.out.println(newValue);
                 if(newValue.booleanValue()){
                     p.setStuSex("男");
                 }else{
                     p.setStuSex("女");
                 }
             }else if (property.equals("stuAge")){
                 String newValue = (String)value;
                 if(newValue.equals("")){
                     return ;
                 }
                 p.setStuAge(newValue);
             }else if (property.equals("stuAddr")){
                 String newValue = (String)value;
                 if(newValue.equals("")){
                     return ;
                 }
                 p.setStuAddr(newValue);
             }else{
                 throw new RuntimeException("错误列名:" + property);
             }
             tv.update(p, null);
         }
         
     }

 使用如下:

private void createTableViewer(Composite parent) {
		viewer = new TableViewer(parent, SWT.MULTI | SWT.H_SCROLL | SWT.V_SCROLL|SWT.FULL_SELECTION|SWT.BORDER);
		Table table=viewer.getTable();
		facadeTableViewer(table);
		
		TableLayout layout=new TableLayout();
		table.setLayout(layout);
		
		layout.addColumnData(new ColumnWeightData(50));
		
		viewer.setContentProvider(new ViewContentProvider());
		viewer.setLabelProvider(new ViewLabelProvider());
		viewer.setSorter(new PersonCompartor());
		viewer.setInput(InitData.init());
		
		
		//设置各列的属性的名称
		String[] columnProperties=new String[]{"stuNo","stuName","stuAge","stuSex","stuAddr"};
		viewer.setColumnProperties(columnProperties);
		//设置修改的编辑列
		CellEditor[] cellEditor = new CellEditor[5];   
        cellEditor[0] = null;   
        cellEditor[1] = new ComboBoxCellEditor(table,PersonCellModifier.NAMES,SWT.READ_ONLY);   
        //年龄需要验证数字
        cellEditor[2] = new TextCellEditor(table);   
        
        cellEditor[3] = new CheckboxCellEditor(table);   
        cellEditor[4] = new TextCellEditor(table);   
		viewer.setCellEditors(cellEditor);
	
		//设置修改器
		ICellModifier  modifier=new PersonCellModifier(viewer);
		viewer.setCellModifier(modifier);
		//验证数字
		Text text = (Text)cellEditor[2].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;
					}
				}
			}
		});
		
		
		
	}

 

相关标签: Eclipse