JAVA.SWT/JFace: SWT高级控件之表格(Table、TableItem和TableColumn)
2010年07月31日 星期六 23:18
《Eclipse SWT/JFACE 核心应用》 清华大学出版社 9.9 表格(Table、TableItem和TableColumn)
SWT中的表格是用Table、TableItem和TableColumn对象来表示数据。
书中删除TableItem时,未删除绑定的Text和CCombo,在下面的代码中做了修正。
import java.util.Hashtable;
public class TableSample {
public class TableItemControls{
Text text;
CCombo combo;
TableEditor tableeditor;
TableEditor tableeditor1;
public TableItemControls(Text text, CCombo combo,
TableEditor tableeditor, TableEditor tableeditor1) {
// super();
this.text = text;
this.combo = combo;
this.tableeditor = tableeditor;
this.tableeditor1 = tableeditor1;
}
public void dispose()
{
text.dispose();
combo.dispose();
tableeditor.dispose();
tableeditor1.dispose();
}
};
private Shell sShell = null; // @jve:decl-index=0:visual-constraint="10,10"
private ViewForm viewForm = null;
private ToolBar toolBar = null;
private Composite composite = null;
private Table table = null;
private Menu menu = null;
private Hashtable tablecontrols = new Hashtable();
//创建ViewForm面板放置工具栏和表格
private void createViewForm() {
viewForm = new ViewForm(sShell, SWT.NONE);
viewForm.setTopCenterSeparate(true);
createToolBar();
viewForm.setTopLeft(toolBar);
createComposite();
viewForm.setContent(composite);
}
//创建工具栏
private void createToolBar() {
toolBar = new ToolBar(viewForm, SWT.FLAT);
final ToolItem add = new ToolItem(toolBar, SWT.PUSH);
add.setText("添加");
add.setImage( ImageFactory.loadImage( toolBar.getDisplay() , ImageFactory.ADD_OBJ));
final ToolItem del = new ToolItem(toolBar, SWT.PUSH);
del.setText("删除");
del.setImage( ImageFactory.loadImage( toolBar.getDisplay() , ImageFactory.DELETE_OBJ));
final ToolItem back = new ToolItem(toolBar, SWT.PUSH);
back.setText("上移");
back.setImage( ImageFactory.loadImage( toolBar.getDisplay() , ImageFactory.BACKWARD_NAV));
final ToolItem forward = new ToolItem(toolBar, SWT.PUSH);
forward.setText("下移");
forward.setImage( ImageFactory.loadImage( toolBar.getDisplay() , ImageFactory.FORWARD_NAV));
final ToolItem save = new ToolItem(toolBar, SWT.PUSH);
save.setText("保存");
save.setImage( ImageFactory.loadImage( toolBar.getDisplay() , ImageFactory.SAVE_EDIT));
//工具栏按钮事件处理
Listener listener = new Listener(){
public void handleEvent(Event event) {
//如果单击添加按钮,添加一行,在实际的项目实现中通常是接收输入的参数,案后添加
//这里为了简单起见,添加固定的一条记录
if ( event.widget == add )
{