JAVA.SWT/JFace: SWT高级控件之表格(四)

2014-11-24 02:33:33 · 作者: · 浏览: 11
= SWT.FILL;
gridData.grabExcessHorizontalSpace = true;
gridData.grabExcessVerticalSpace = true;
gridData.verticalAlignment = SWT.FILL;

//创建表格,使用SWT.FULL_SELECTION样式,可同时选中一行
table = new Table(composite, SWT.MULTI|SWT.FULL_SELECTION|SWT.CHECK);
table.setHeaderVisible(true);//设置显示表头
table.setLayoutData(gridData);//设置表格布局
table.setLinesVisible(true);//设置显示表格线/*
//创建表头的字符串数组
String[] tableHeader = {"姓名","性别","电话","电子邮件"};
for (int i=0;i TableColumn tableColumn = new TableColumn(table, SWT.NONE);
tableColumn.setText( tableHeader[i]);
//设置表头可移动,默认为false
tableColumn.setMoveable(true);
}
//添加三行数据
TableItem item = new TableItem(table, SWT.NONE);
item.setText(new String[] {"张三", "男","123","zhangsan@sina.com"});
//设置图标
//item.setImage( ImageFactory.loadImage( table.getDisplay(),ImageFactory.ICON_BOY));

for (int i = 0; i < 5; i++) {
item = new TableItem(table, SWT.NONE);
item.setText(new String[] {"李四", "男","4582","lisi@sina.com"});
}
//设置图标
//item.setImage( ImageFactory.loadImage( table.getDisplay(),ImageFactory.ICON_BOY));

item = new TableItem(table, SWT.NONE);
item.setText(new String[] {"周五", "女","567","zhouwu@sina.com"});
//设置图标
//item.setImage( ImageFactory.loadImage( table.getDisplay(),ImageFactory.ICON_GIRL));

//添加可编辑的单元格
// /******************************************************
TableItem [] items = table.getItems ();
for (int i=0; i //第一列设置,创建TableEditor对象
final TableEditor editor = new TableEditor (table);
//创建一个文本框,用于输入文字
final Text text = new Text (table, SWT.NONE);
//将文本框当前值,设置为表格中的值
text.setText(items[i].getText(0));
//设置编辑单元格水平填充
editor.grabHorizontal = true;
//关键方法,将编辑单元格与文本框绑定到表格的第一列
editor.setEditor(text, items[i], 0);
//当文本框改变值时,注册文本框改变事件,该事件改变表格中的数据。
//否则即使改变的文本框的值,对表格中的数据也不会影响
text.addModifyListener( new ModifyListener(){
public void modifyText(ModifyEvent e) {
editor.getItem().setText(1,text.getText());
}

});
//同理,为第二列绑定下来框CCombo
final TableEditor editor1 = new TableEditor (table);
final CCombo combo = new CCombo (table, SWT.NONE);
combo.add("男");
combo.add("女");
combo.setText(items[i].getText(1));
editor1.grabHorizontal = true;
editor1.setEditor(combo, items[i], 1);
combo.addModifyListener( new ModifyListener(){
public void modifyText(ModifyEvent e) {
editor1.getItem().setText(1,combo.getText());
}

});

// 保存TableItem与绑定Control的对应关系,删除TableItem时使用
TableItemControls cons = new TableItemControls(text, combo, editor, editor1);
tablecontrols.put(items[i], cons);

}
// *****************************************************/
// /***************************************************
//创建TableCursor对象,使用上下左右键可以控制表格
final TableCursor cursor = new Tabl