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

全部博文(13)

文章存档

2008年(13)

我的朋友

分类: Java

2008-05-31 11:36:41

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

public class Person {
    private int id ;
    private String name ;
    private String sex ;
    private int age ;
    private Date createDate ;
    
    public Person(int id,String name,String sex,int age,Date createDate){
        this.id = id ;
        this.name = name ;
        this.sex = sex ;
        this.age = age ;
        this.createDate = createDate ;
    }
    
    //初始化person
    public static List<Person> getPerson(){
        List<Person> list = new ArrayList<Person>() ;
        int n=10;
        for(int i=0;i<n;i++){
            int id = i ;
            String name = "帅哥"+i ;
            String sex = i%2==0? "男" : "女" ;
            int age = i+20 ;
            Date d = new Date() ;
            Person p = new Person(id,name,sex,age,d) ;
            list.add(p) ;
        }
        return list ;
    }
    
    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getSex() {
        return sex;
    }
    public void setSex(String sex) {
        this.sex = sex;
    }
    public int getAge() {
        return age;
    }
    public void setAge(int age) {
        this.age = age;
    }
    public Date getCreateDate() {
        return createDate;
    }
    public void setCreateDate(Date createDate) {
        this.createDate = createDate;
    }
}

Person.java中定义了几个属性,并且初始化10个person对象

程序的核心:

内容提供器MyContentProvider.java

import java.util.List;

import org.eclipse.jface.viewers.IStructuredContentProvider;
import org.eclipse.jface.viewers.Viewer;

public class MyContentProvider implements IStructuredContentProvider {

    @Override
    public Object[] getElements(Object inputElement) {
        // TODO Auto-generated method stub
        if(inputElement instanceof List){
            return ((List)inputElement).toArray() ;
        }
        else{
            return new Object[0] ;
        }
    }

    @Override
    public void dispose() {
        // TODO Auto-generated method stub
    }

    @Override
    public void inputChanged(Viewer arg0, Object arg1, Object arg2) {
        // TODO Auto-generated method stub
    }
}

有点惭愧,到目前为止,我还不知道各字段的含义,只是参考了几部分代码,这个内容提供器的代码都是一样的.

标签提供器:MyLabelProvider.java

注意,必须要实现这个接口:ITableLabelProvider,否则表格内容会出现异常!


import java.text.SimpleDateFormat;

import org.eclipse.jface.viewers.ITableLabelProvider;
import org.eclipse.jface.viewers.LabelProvider;
import org.eclipse.swt.graphics.Image;

import pack1.People;

public class MyLabelProvider extends LabelProvider implements ITableLabelProvider {

    public String getColumnText(Object element, int columnIndex) {
        if (element instanceof Person){
            Person p = (Person)element;
            if(columnIndex == 0){
                return p.getId()+"" ;
            }else if(columnIndex == 1){
                return p.getName() +"";
            }else if (columnIndex ==2){
                return p.getSex()+"";
            }else if (columnIndex == 3){
                return p.getAge()+"" ;
            }else if (columnIndex == 4){
                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
                return sdf.format(p.getCreateDate())+"";
            }
        }
        return null;
    }
    
    
    public Image getColumnImage(Object element,int columnIndex){
        return null ;
    }
}

ok,大功告成了!总结jface使用TableViewer的步骤:

1.初始化表格;

2.设置两个提供器:content,label

3.表格数据对象的pojo类

程序界面如下:

 
阅读(1305) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~