前边实现了一个表格的基本功能,但这并不够好,能否为表格实现一些更好的功能呢?答案是肯定的。下边我们来加入直接编辑的功能。
一、要实现这个功能必须提供一个实现ICellModifier的类。内容如下
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("错误列名:" + property);
}
tv.update(p, null);
}
}
二、好了,有了这个类,下一部就是如何把它和TestTableViewer关联起来,在TestTableViewer中setInput()后加入如下内容
tableViewer.setColumnProperties(newString[]{"id","name","sex","age","createDate"});
CellEditor[]cellEditor=newCellEditor[5];
cellEditor[0]=null;
cellEditor[1]=newComboBoxCellEditor(tableViewer.getTable(),MyCellModifier.NAMES,SWT.READ_ONLY);
cellEditor[2]=newCheckboxCellEditor(tableViewer.getTable());
cellEditor[3]=newTextCellEditor(tableViewer.getTable());
cellEditor[4]=null;
tableViewer.setCellEditors(cellEditor);
ICellModifiermodifier=newMyCellModifier(tableViewer);
tableViewer.setCellModifier(modifier);
我们让名字这一列用下拉条来编辑,让性别这一列变成类似checkbox的操作,让年龄这一类变成直接输入
ok,尝试一下。
三、问题出现,如果年龄的地方我们输入一个非数字呢,所以为了安全起见,我们加入一个验证器,禁止用户输入非数字
在上边的内容下加入
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;
}
}
}
});
好了,再试试是否不能输入非整数了?解决。其实还是有些问题的,试着输入个0,呵呵。这里就需要你自己按照自己的实际需求来实现了。
但作为demo这个的目的已经达到了。
分享到:
相关推荐
SWT/Jface 开发入门指南 介绍以下方面的内容: ? 设定 swt 以及 JFace 的开发环境 ? swt 的一些简单部件(widget)介绍 ? JFace 的一些入门性介绍 ? swt 和 JFace 的事件模式 ? 通过一些简单的例子说明如何利用 swt ...
SWT/Jface API 3.4 SWT/Jface API 3.4 SWT/Jface API 3.4 SWT/Jface API 3.4
Eclipse SWT/JFace SWT JFace 实例 Eclipse SWT/JFace 书中 全部实例
SWT/JFACE API,是关于SWT/JFACE开发所用的API
swt/Jface用到的最基本的jar包
Eclipse SWT/JFace 核心应用光盘源码下载,解压后即可使用
NULL 博文链接:https://1299020954.iteye.com/blog/2231923
Eclipse SWT/JFace 核心应用 带书签 Eclipse SWT/JFace
Eclipse SWT/JFace核心应用
NULL 博文链接:https://marsvaadin.iteye.com/blog/1320182
本文档为swt/jface的api中文帮助文档。
SWT/JFace开发实例
eclipse swt/jface核心应用源代码 带视频,找了好久,希望可以帮到大家,跟书匹配
eclipse swt/jface核心应用 这本书的源代码
Eclipse从入门到精通中的SWT/JFace章节!
NULL 博文链接:https://niub.iteye.com/blog/1806580
NULL 博文链接:https://arizona.iteye.com/blog/526298
中国Eclipse社区的SWT/JFace开发入门指南,很经典的,网页上还有原作者的博客上的图片不能显示了,通过整理找到了图片,并做成了PDF的。
基于 Java 桌面程序开发的图形库主要有 3 种,它们分别是 AWT、Swing 和 SWT。用前 两种库编写的桌面程序不够美观而且执行效率低,响应速度慢,SWT 恰好克服了 AWT 和 Swing 的缺点,它丰富的组件可以使程序员开发出...