TableTree/TableTreeEditor


import java.awt.Toolkit;


import org.eclipse.jface.window.ApplicationWindow;
import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.TableTree;
import org.eclipse.swt.custom.TableTreeEditor;
import org.eclipse.swt.custom.TableTreeItem;
import org.eclipse.swt.events.ModifyEvent;
import org.eclipse.swt.events.ModifyListener;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.layout.GridData;
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;
import org.eclipse.swt.widgets.Text;


@SuppressWarnings("deprecation")
public class TableTreeTest extends ApplicationWindow {

private TableTree tableTree;


private static final String[] COLUMN_NAME={"name","ismi","msisdn","uehost"};
public TableTreeTest() {
super(null);
}


@Override
protected void configureShell(Shell shell) {
super.configureShell(shell);
shell.setImage(new Image(shell.getDisplay(), "main_title.jpg"));
shell.setText("TableTree示例");
int screenW = Toolkit.getDefaultToolkit().getScreenSize().width;
int screenH = Toolkit.getDefaultToolkit().getScreenSize().height;
shell.setSize(800, 500);
int shellW = shell.getBounds().width;
int shellH = shell.getBounds().height;
shell.setLocation((screenW - shellW) / 2, (screenH - shellH) / 2);
}


@Override
protected Control createContents(Composite parentShell) {
tableTree = new TableTree(parentShell, SWT.FULL_SELECTION);
tableTree.setLayoutData(new GridData(GridData.FILL_BOTH));
Table table = tableTree.getTable();
table.setHeaderVisible(true);
table.setLinesVisible(true);
// 設置表頭
for (int i = 0; i < COLUMN_NAME.length; i++) {
TableColumn tableColumn = new TableColumn(table, SWT.LEFT);
tableColumn.setText(COLUMN_NAME[i]);
}
// 初始化表格數據
{
TableTreeItem parent = new TableTreeItem(tableTree, SWT.NONE);
parent.setText(0, "AtpCfg ");
// Add children items
{
{// Create a child item and add data to the columns
TableTreeItem child_auto = new TableTreeItem(parent,SWT.NONE);
{
child_auto.setText(0, "Auto");
TableTreeItem child_child = new TableTreeItem(child_auto, SWT.NONE);
child_child.setText(0, "Ue:name");
child_child.setText(1, "imsi");
child_child.setText(2, "msisdn");
child_child.setText(3, "uehost");
{
TableTreeItem child_child_child = new TableTreeItem(child_child, SWT.NONE);
child_child_child.setText(0, "HSPA-1");
child_child_child.setText(1, "460990099992825");
child_child_child.setText(2, "13912342825");
child_child_child.setText(3, "AirFlux2");
TableTreeItem child_child_child1 = new TableTreeItem(
child_child, SWT.NONE);
child_child_child1.setText(0, "HSPA-2");
child_child_child1.setText(1, "460990099992825");
child_child_child1.setText(2, "13912342825");
child_child_child1.setText(3, "AirFlux2");
}
}
}
{
TableTreeItem child_NE = new TableTreeItem(parent, SWT.NONE);
{
child_NE.setText(0, "NE");
TableTreeItem child_child1 = new TableTreeItem(child_NE, SWT.NONE);
child_child1.setText(0, "FtpServer:name");
child_child1.setText(1, "user");
child_child1.setText(2, "pwd");
child_child1.setText(3, "FtpsrlIP");
{
TableTreeItem child_child_child11 = new TableTreeItem(child_child1, SWT.NONE);
child_child_child11.setText(0, "FTPDL1");
child_child_child11.setText(1, "download ");
child_child_child11.setText(2, "download ");
child_child_child11.setText(3, "18.1.1.2");
TableTreeItem child_child_child12 = new TableTreeItem(child_child1, SWT.NONE);
child_child_child12.setText(0, "FTPDL2");
child_child_child12.setText(1, "download ");
child_child_child12.setText(2, "download ");
child_child_child12.setText(3, "18.1.1.2");
}
}
}
}
// 展開指定
parent.setExpanded(true);// 父節點展開
}




for (int i = 0; i < COLUMN_NAME.length; i++) {
final int editorCol=i;
final TableTreeEditor tableEditor = new TableTreeEditor(tableTree);
tableEditor.horizontalAlignment = SWT.LEFT;
tableEditor.grabHorizontal = true;
tableEditor.minimumWidth = 50;
tableTree.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(SelectionEvent event) {
Control oldEditor = tableEditor.getEditor();
if (oldEditor != null){
oldEditor.dispose();
}
TableTreeItem item = (TableTreeItem) event.item;
if (item == null){
return;
}
Text newEditor = new Text(tableTree.getTable(), SWT.NONE);
String content =item.getText(editorCol);
if(content!=null && !"".equals(content)){
newEditor.setText(item.getText(editorCol));
}else{
newEditor.setText("");
return;
}
newEditor.addModifyListener(new ModifyListener() {
public void modifyText(ModifyEvent e) {
Text text = (Text) tableEditor.getEditor();
tableEditor.getItem().setText(editorCol, text.getText());
}
});
newEditor.selectAll();
newEditor.setFocus();
tableEditor.setEditor(newEditor, item, editorCol);
}
});
}


//刷新表格,不刷新,數據不會顯示
TableColumn[] tableColumns=tableTree.getTable().getColumns();
for(int i=0;i<tableColumns.length;i++){
tableColumns[i].pack();
tableColumns[i].setWidth(150);//設置表格列寬度
}
return parentShell;
}


public static void main(String[] args) {
TableTreeTest test = new TableTreeTest();
test.setBlockOnOpen(true);
test.open();
Display.getCurrent().dispose();
}


}
發佈了60 篇原創文章 · 獲贊 18 · 訪問量 10萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章