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

全部博文(13)

文章存档

2008年(13)

我的朋友

分类: Java

2008-05-31 11:26:28

在操作jface的表格之前,推荐以下两篇文章:
这个将得很详细,我这里示例的代码是参照这篇文章完成的.
这篇只是大概的介绍了使用comboxCellEditor的过程,个人比较推荐第一个blog中介绍的内容.
 
在上次开发中,我需要在表格中添加combo下拉控件,之前已经用swt完成了,思路是这样的:
1.添加表格;
2.添加combo控件 ;
3.添加tableEditor控件 ;
4.绑定tableEditor和combo控件 ;
5.绑定到表格 .
在实际的使用中,发现了很严重的问题:
1.需要做大量的循环;2.代码重复出现;3.高度耦合.
 
在学习了jface之后,发现有一个编辑器,正是我所需要的:ComboBoxCellEditor
ok,这里我将讲述使用jface搭建一个表格的过程.
程序有5个文件:
TableWindow.java  程序入口
TableConfigure.java  表格配置
Person.java  表格内容对象
MyContentProvider.java  内容提供器
MyLableProvider.java  标签提供器
 
代码如下:
 
TableWindow.java

import org.eclipse.jface.viewers.TableViewer;
import org.eclipse.jface.window.ApplicationWindow;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableColumn;

public class TableWindow extends ApplicationWindow {

    private TableViewer tableViewer ;
    public static Table table ;
    private static TableWindow app ;
    
    public TableWindow(){
        super(null) ;
        app = this ;
    }
    protected void configureShell(Shell shell){
        super.configureShell(shell) ;
        shell.setSize(520,300) ;
        shell.setText("重写表格示例") ;
    }
    
    protected Control createContents(Composite parent){
        //初始化表格
        initTable() ;
        //设置内容提供器
        tableViewer.setContentProvider(new MyContentProvider()) ;
        //设置标签提供器
        tableViewer.setLabelProvider(new MyLabelProvider());
        //添加数据集
        tableViewer.setInput(Person.getPerson()) ;
        return parent ;
    }
    
    public static void main(String[] args){
        TableWindow window = new TableWindow() ;
        window.setBlockOnOpen(true) ;
        window.open() ;
        Display.getCurrent().dispose() ;
    }
    
    public static TableWindow getApp(){
        return app ;
    }
    
    protected void initTable(){
        tableViewer = new TableViewer(TableWindow.getApp().getShell(),
                SWT.MULTI | SWT.FULL_SELECTION | SWT.BORDER|SWT.V_SCROLL|SWT.H_SCROLL) ;
        table = tableViewer.getTable() ;
        for(int i=0;i<TableConfigure.COLUMN_NAME.length;i++){
            new TableColumn(table,SWT.FLAT).setText(TableConfigure.COLUMN_NAME[i]) ;
            table.getColumn(i).setWidth(100) ;
        }
        table.setHeaderVisible(true) ;
        table.setLinesVisible(true) ;
    }
}

TableConfigure.java 添加表格配置

public class TableConfigure {
    public final static int ID = 0 ;
    public final static int NAME = 1 ;
    public final static int SEX = 2 ;
    public final static int AGE = 3 ;
    public final static int DATE = 4 ;
    
    public final static String[] COLUMN_NAME=new String[]{
        "id号" ,
        "姓名" ,
        "性别" ,
        "年龄" ,
        "日期"
    } ;
}

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