一提到表格,人們就想得到EXCEL。不錯,這個優秀的軟件一共提供了幾千個功能點,但人們平常一般只用到其常用的幾十個功能。
SWT/JFACE提供的表格雖然不能完成EXCEL的所有功能,但其常用的功能已經具備。比如:雙擊編輯表格、操作單元格、在單元格中加入控件。
其關鍵就在于CellEditor的使用!

for (int i=0; i<items.length; i++)
{
TableEditor editor = new TableEditor (table);
CCombo combo = new CCombo (table, SWT.NONE);
combo.setText("CCombo");
combo.add("item 1");
combo.add("item 2");
editor.grabHorizontal = true;
editor.setEditor(combo, items[i], 0);
editor = new TableEditor (table);
Text text = new Text (table, SWT.NONE);
text.setText("Text");
editor.grabHorizontal = true;
editor.setEditor(text, items[i], 1);
editor = new TableEditor (table);
Button button = new Button (table, SWT.CHECK);
button.pack ();
editor.minimumWidth = button.getSize ().x;
editor.horizontalAlignment = SWT.LEFT;
editor.setEditor (button, items[i], 2);
}
用以上代碼加入CCombo和Check。
關鍵在于:
editor.setEditor(combo, items[i], 0);
combo為控件對象,item[i]和0為確定editor(單元格)的位置。
有了這個函數就可以任意加控件了。特殊情況加入text控件就可實現雙擊編輯,代碼如下:

table.addSelectionListener(new SelectionAdapter()
{

public void widgetSelected(SelectionEvent e)
{
// Clean up any previous editor control
Control oldEditor = editor.getEditor();
if (oldEditor != null) oldEditor.dispose();
// Identify the selected row
TableItem item = (TableItem)e.item;
if (item == null) return;
// The control that will be the editor must be a child of the Table
Text newEditor = new Text(table, SWT.NONE);
newEditor.setText(item.getText(EDITABLECOLUMN));

newEditor.addModifyListener(new ModifyListener()
{

public void modifyText(ModifyEvent me)
{
Text text = (Text)editor.getEditor();
editor.getItem().setText(EDITABLECOLUMN, text.getText());
}
});
newEditor.selectAll();
newEditor.setFocus();
editor.setEditor(newEditor, item, EDITABLECOLUMN);
}
});
哈哈 這樣就可以了哦!
SWT/JFACE提供的表格雖然不能完成EXCEL的所有功能,但其常用的功能已經具備。比如:雙擊編輯表格、操作單元格、在單元格中加入控件。
其關鍵就在于CellEditor的使用!






















關鍵在于:
editor.setEditor(combo, items[i], 0);
combo為控件對象,item[i]和0為確定editor(單元格)的位置。
有了這個函數就可以任意加控件了。特殊情況加入text控件就可實現雙擊編輯,代碼如下:































