SWT 修改表格单元格内容
package com.ui;
import org.eclipse.swt.*;
public class TableTreeSample {
public static void main (String [] args) {
Display display = new Display ();
Shell shell = new Shell(display);
shell.setText( "TableTree Sample 1 ");
shell.setLayout(new FillLayout());
final Table table = new Table(shell, SWT.BORDER|SWT.FULL_SELECTION);
String[] cols = { "234 ", "234 "};
for(int i=0;i <cols.length;i++){
TableColumn col = new TableColumn(table,SWT.LEFT);
col.setText(cols[i]);
col.setWidth(100);
}
table.setHeaderVisible(true);
table.setLinesVisible(true);
TableItem item1 = new TableItem(table,SWT.NULL);
item1.setText(0, "0 ");
item1.setText(1, "点这里修改");
TableItem item2 = new TableItem(table,SWT.NULL);
item2.setText(0, "1");
item2.setText(1, "点这里修改");
final TableEditor tableEditor = new TableEditor(table);
tableEditor.grabHorizontal = true;
table.addSelectionListener(new SelectionAdapter() {
// EDIT_COLUMN 是判断修改的第几列!!!如果想每列都可以修改。可以循环。
private static final int EDIT_COLUMN = 0;
public void widgetSelected(SelectionEvent e){
// EDIT_COLUMN 是判断鼠标选中第几行!!!
int index = table.getSelectionIndex();
System.out.println("这个事是第几行~~"+index);
if(index == -1){
return;
}
table.setSelection(new int[0]);
TableItem item = table.getItem(index);
final Text text = new Text(table, SWT.NONE);
text.setText(item.getText(EDIT_COLUMN));
text.addFocusListener(new FocusAdapter(){
public void focusLost(FocusEvent e){
TableItem item = tableEditor.getItem();
item.setText(EDIT_COLUMN,text.getText());
text.dispose();
}
});
tableEditor.setEditor (text, item, EDIT_COLUMN);
text.setFocus();
text.selectAll();
}
});
shell.setSize(250,100);
shell.open();
while (!shell.isDisposed ()){
if (!display.readAndDispatch ()){
display.sleep ();
}
}
display.dispose ();
}
}