SWT Table示例

来源:互联网 发布:软件好学吗 编辑:程序博客网 时间:2024/04/27 13:40
[java] view plain copy
  1. /* 
  2.  * 文件名:TableSample.java 
  3.  * 版权:Copyright 1986-2012 Andy. All Rights Reserved.  
  4.  * 描述: TableSample.java 
  5.  * 修改人:Andy 
  6.  * 修改时间:2012-10-26 
  7.  * 修改内容:新增 
  8.  */  
  9. package org.wang.andy.swtjface.demo.table;  
  10.   
  11. import java.util.Hashtable;  
  12.   
  13. import org.eclipse.swt.SWT;  
  14. import org.eclipse.swt.custom.CCombo;  
  15. import org.eclipse.swt.custom.ControlEditor;  
  16. import org.eclipse.swt.custom.TableCursor;  
  17. import org.eclipse.swt.custom.TableEditor;  
  18. import org.eclipse.swt.custom.ViewForm;  
  19. import org.eclipse.swt.events.FocusEvent;  
  20. import org.eclipse.swt.events.FocusListener;  
  21. import org.eclipse.swt.events.KeyEvent;  
  22. import org.eclipse.swt.events.KeyListener;  
  23. import org.eclipse.swt.events.ModifyEvent;  
  24. import org.eclipse.swt.events.ModifyListener;  
  25. import org.eclipse.swt.events.MouseEvent;  
  26. import org.eclipse.swt.events.MouseListener;  
  27. import org.eclipse.swt.events.SelectionAdapter;  
  28. import org.eclipse.swt.events.SelectionEvent;  
  29. import org.eclipse.swt.graphics.Image;  
  30. import org.eclipse.swt.layout.FillLayout;  
  31. import org.eclipse.swt.layout.GridData;  
  32. import org.eclipse.swt.layout.GridLayout;  
  33. import org.eclipse.swt.widgets.Composite;  
  34. import org.eclipse.swt.widgets.Display;  
  35. import org.eclipse.swt.widgets.Event;  
  36. import org.eclipse.swt.widgets.Listener;  
  37. import org.eclipse.swt.widgets.Menu;  
  38. import org.eclipse.swt.widgets.MenuItem;  
  39. import org.eclipse.swt.widgets.Shell;  
  40. import org.eclipse.swt.widgets.Table;  
  41. import org.eclipse.swt.widgets.TableColumn;  
  42. import org.eclipse.swt.widgets.TableItem;  
  43. import org.eclipse.swt.widgets.Text;  
  44. import org.eclipse.swt.widgets.ToolBar;  
  45. import org.eclipse.swt.widgets.ToolItem;  
  46.   
  47. /** 
  48.  * TODO 添加类的一句话简单描述。 
  49.  * <p> 
  50.  * TODO 详细描述 
  51.  * <p> 
  52.  * TODO 示例代码 
  53.  *  
  54.  * <pre> 
  55.  * </pre> 
  56.  *  
  57.  */  
  58.   
  59. public class TableSample  
  60. {  
  61.   
  62.     public class TableItemControls  
  63.     {  
  64.         Text text;  
  65.   
  66.         CCombo combo;  
  67.   
  68.         TableEditor tableeditor;  
  69.   
  70.         TableEditor tableeditor1;  
  71.   
  72.         public TableItemControls(Text text, CCombo combo,  
  73.                 TableEditor tableeditor, TableEditor tableeditor1)  
  74.         {  
  75.             // super();  
  76.             this.text = text;  
  77.             this.combo = combo;  
  78.             this.tableeditor = tableeditor;  
  79.             this.tableeditor1 = tableeditor1;  
  80.         }  
  81.   
  82.         public void dispose()  
  83.         {  
  84.             text.dispose();  
  85.             combo.dispose();  
  86.             tableeditor.dispose();  
  87.             tableeditor1.dispose();  
  88.         }  
  89.     };  
  90.   
  91.     private Shell sShell = null// @jve:decl-index=0:visual-constraint="10,10"  
  92.   
  93.     private ViewForm viewForm = null;  
  94.   
  95.     private ToolBar toolBar = null;  
  96.   
  97.     private Composite composite = null;  
  98.   
  99.     private Table table = null;  
  100.   
  101.     private Menu menu = null;  
  102.   
  103.     private Hashtable<TableItem, TableItemControls> tablecontrols = new Hashtable<TableItem, TableItemControls>();  
  104.   
  105.     // 创建ViewForm面板放置工具栏和表格  
  106.     private void createViewForm()  
  107.     {  
  108.         viewForm = new ViewForm(sShell, SWT.NONE);  
  109.         // viewForm.setTopCenterSeparate(true);  
  110.         createToolBar();  
  111.         viewForm.setTopLeft(toolBar);  
  112.         createComposite();  
  113.         viewForm.setContent(composite);  
  114.     }  
  115.   
  116.     // 创建工具栏  
  117.     private void createToolBar()  
  118.     {  
  119.         toolBar = new ToolBar(viewForm, SWT.FLAT);  
  120.         final ToolItem add = new ToolItem(toolBar, SWT.PUSH);  
  121.         add.setText("添加");  
  122.         add.setImage(new Image(toolBar.getDisplay(), "icons//add.gif"));  
  123.         final ToolItem del = new ToolItem(toolBar, SWT.PUSH);  
  124.         del.setText("删除");  
  125.         del.setImage(new Image(toolBar.getDisplay(), "icons//delete.gif"));  
  126.         final ToolItem back = new ToolItem(toolBar, SWT.PUSH);  
  127.         back.setText("上移");  
  128.         back.setImage(new Image(toolBar.getDisplay(), "icons//up.gif"));  
  129.         final ToolItem forward = new ToolItem(toolBar, SWT.PUSH);  
  130.         forward.setText("下移");  
  131.         forward.setImage(new Image(toolBar.getDisplay(), "icons//down.gif"));  
  132.         final ToolItem save = new ToolItem(toolBar, SWT.PUSH);  
  133.         save.setText("保存");  
  134.         save.setImage(new Image(toolBar.getDisplay(), "icons//save.gif"));  
  135.         // 工具栏按钮事件处理  
  136.         Listener listener = new Listener()  
  137.         {  
  138.             @Override  
  139.             public void handleEvent(Event event)  
  140.             {  
  141.                 // 如果单击添加按钮,添加一行,在实际的项目实现中通常是接收输入的参数,案后添加  
  142.                 // 这里为了简单起见,添加固定的一条记录  
  143.                 if (event.widget == add)  
  144.                 {  
  145.                     TableItem item = new TableItem(table, SWT.NONE);  
  146.                     item.setText(new String[]{"郑六""女""568"""});  
  147.                 }  
  148.                 // 如果单击删除按钮  
  149.                 else if (event.widget == del)  
  150.                 {  
  151.                     // 首先获得表格中所有的行  
  152.                     TableItem[] items = table.getItems();  
  153.                     // 循环所有行  
  154.                     for (int i = items.length - 1; i >= 0; i--)  
  155.                     {  
  156.                         // 如果该行没有被选中,继续循环  
  157.                         if (!items[i].getChecked())  
  158.                             continue;  
  159.                         // 否则选中,查找该表格中是否有该行  
  160.                         int index = table.indexOf(items[i]);  
  161.                         // 如果没有该行,继续循环  
  162.                         if (index < 0)  
  163.                             continue;  
  164.                         // 删除绑定的控件  
  165.                         TableItemControls cons = tablecontrols  
  166.                                 .get(items[index]);  
  167.                         if (cons != null)  
  168.                         {  
  169.                             cons.dispose();  
  170.                             tablecontrols.remove(items[index]);  
  171.                             System.out.println("dispose " + index);  
  172.                         }  
  173.                         // 如果有该行,删除该行  
  174.                         // items[index].dispose();  
  175.                         table.remove(index);  
  176.                         System.out.println("i=" + i + ", index=" + index);  
  177.                         System.out.println("行数:" + table.getItemCount());  
  178.                         // table.pack();  
  179.                     }  
  180.                 }  
  181.                 // 如果单击上移按钮  
  182.                 else if (event.widget == back)  
  183.                 {  
  184.                     int selectedRow = table.getSelectionIndex();  
  185.                     if (selectedRow > 0)  
  186.                         table.setSelection(selectedRow - 1);// 设置选中的行数  
  187.                 }  
  188.                 // 如果单击下移按钮  
  189.                 else if (event.widget == forward)  
  190.                 {  
  191.                     int selectedRow = table.getSelectionIndex();  
  192.                     if (selectedRow > -1  
  193.                             && selectedRow < table.getItemCount() - 1)  
  194.                         table.setSelection(selectedRow + 1);// 设置选中的行数  
  195.                 }  
  196.                 // 如果单击保存按钮  
  197.                 else if (event.widget == save)  
  198.                 {  
  199.                     TableItem[] items = table.getItems();  
  200.                     // 保存到文件或数据库中,数据持久化,这里省略  
  201.                     for (int i = 0; i < items.length; i++)  
  202.                         for (int j = 0; j < table.getColumnCount(); j++)  
  203.                             System.out.println(items[i].getText(j));  
  204.                 }  
  205.             }  
  206.   
  207.         };  
  208.         // 为工具栏的按钮注册事件  
  209.         add.addListener(SWT.Selection, listener);  
  210.         del.addListener(SWT.Selection, listener);  
  211.         back.addListener(SWT.Selection, listener);  
  212.         forward.addListener(SWT.Selection, listener);  
  213.         save.addListener(SWT.Selection, listener);  
  214.     }  
  215.   
  216.     // 创建放置表格的面板  
  217.     private void createComposite()  
  218.     {  
  219.         GridLayout gridLayout = new GridLayout();  
  220.         gridLayout.numColumns = 1;  
  221.         composite = new Composite(viewForm, SWT.NONE);  
  222.         composite.setLayout(gridLayout);  
  223.         createTable();  
  224.     }  
  225.   
  226.     // 创建表格  
  227.     private void createTable()  
  228.     {  
  229.         // 表格布局  
  230.         GridData gridData = new org.eclipse.swt.layout.GridData();  
  231.         gridData.horizontalAlignment = SWT.FILL;  
  232.         gridData.grabExcessHorizontalSpace = true;  
  233.         gridData.grabExcessVerticalSpace = true;  
  234.         gridData.verticalAlignment = SWT.FILL;  
  235.   
  236.         // 创建表格,使用SWT.FULL_SELECTION样式,可同时选中一行  
  237.         table = new Table(composite, SWT.MULTI | SWT.FULL_SELECTION | SWT.CHECK);  
  238.         table.setHeaderVisible(true);// 设置显示表头  
  239.         table.setLayoutData(gridData);// 设置表格布局  
  240.         table.setLinesVisible(true);// 设置显示表格线/*  
  241.         // 创建表头的字符串数组  
  242.         String[] tableHeader = {"姓名""性别""电话""电子邮件"};  
  243.         for (int i = 0; i < tableHeader.length; i++)  
  244.         {  
  245.             TableColumn tableColumn = new TableColumn(table, SWT.NONE);  
  246.             tableColumn.setText(tableHeader[i]);  
  247.             // 设置表头可移动,默认为false  
  248.             tableColumn.setMoveable(true);  
  249.         }  
  250.         // 添加三行数据  
  251.         TableItem item = new TableItem(table, SWT.NONE);  
  252.         item.setText(new String[]{"张三""男""123"""});  
  253.         // 设置图标  
  254.         // item.setImage( ImageFactory.loadImage(  
  255.         // table.getDisplay(),ImageFactory.ICON_BOY));  
  256.   
  257.         for (int i = 0; i < 5; i++)  
  258.         {  
  259.             item = new TableItem(table, SWT.NONE);  
  260.             item.setText(new String[]{"李四""男""4582"""});  
  261.         }  
  262.         // 设置图标  
  263.         // item.setImage( ImageFactory.loadImage(  
  264.         // table.getDisplay(),ImageFactory.ICON_BOY));  
  265.   
  266.         item = new TableItem(table, SWT.NONE);  
  267.         item.setText(new String[]{"周五""女""567"""});  
  268.         // 设置图标  
  269.         // item.setImage( ImageFactory.loadImage(  
  270.         // table.getDisplay(),ImageFactory.ICON_GIRL));  
  271.   
  272.         // 添加可编辑的单元格  
  273.         // /******************************************************  
  274.         TableItem[] items = table.getItems();  
  275.         for (int i = 0; i < items.length; i++)  
  276.         {  
  277.             // 第一列设置,创建TableEditor对象  
  278.             final TableEditor editor = new TableEditor(table);  
  279.             // 创建一个文本框,用于输入文字  
  280.             final Text text = new Text(table, SWT.NONE);  
  281.             // 将文本框当前值,设置为表格中的值  
  282.             text.setText(items[i].getText(0));  
  283.             // 设置编辑单元格水平填充  
  284.             editor.grabHorizontal = true;  
  285.             // 关键方法,将编辑单元格与文本框绑定到表格的第一列  
  286.             editor.setEditor(text, items[i], 0);  
  287.             // 当文本框改变值时,注册文本框改变事件,该事件改变表格中的数据。  
  288.             // 否则即使改变的文本框的值,对表格中的数据也不会影响  
  289.             text.addModifyListener(new ModifyListener()  
  290.             {  
  291.                 public void modifyText(ModifyEvent e)  
  292.                 {  
  293.                     editor.getItem().setText(1, text.getText());  
  294.                 }  
  295.   
  296.             });  
  297.             // 同理,为第二列绑定下 拉框CCombo  
  298.             final TableEditor editor1 = new TableEditor(table);  
  299.             final CCombo combo = new CCombo(table, SWT.NONE);  
  300.             combo.add("男");  
  301.             combo.add("女");  
  302.             combo.setText(items[i].getText(1));  
  303.             editor1.grabHorizontal = true;  
  304.             editor1.setEditor(combo, items[i], 1);  
  305.             combo.addModifyListener(new ModifyListener()  
  306.             {  
  307.                 public void modifyText(ModifyEvent e)  
  308.                 {  
  309.                     editor1.getItem().setText(1, combo.getText());  
  310.                 }  
  311.   
  312.             });  
  313.   
  314.             // 保存TableItem与绑定Control的对应关系,删除TableItem时使用  
  315.             TableItemControls cons = new TableItemControls(text, combo, editor,  
  316.                     editor1);  
  317.             tablecontrols.put(items[i], cons);  
  318.   
  319.         }  
  320.         // *****************************************************/  
  321.         // /***************************************************  
  322.         // 创建TableCursor对象,使用上下左右键可以控制表格  
  323.         final TableCursor cursor = new TableCursor(table, SWT.NONE);  
  324.         // 创建可编辑的控件  
  325.         final ControlEditor editor = new ControlEditor(cursor);  
  326.         editor.grabHorizontal = true;  
  327.         editor.grabVertical = true;  
  328.         // 为TableCursor对象注册事件  
  329.         cursor.addSelectionListener(new SelectionAdapter()  
  330.         {  
  331.             // 但移动光标,在单元格上单击回车所触发的事件  
  332.             public void widgetDefaultSelected(SelectionEvent e)  
  333.             {  
  334.                 // 创建一个文本框控件  
  335.                 final Text text = new Text(cursor, SWT.NONE);  
  336.                 // 获得当前光标所在的行TableItem对象  
  337.                 TableItem row = cursor.getRow();  
  338.                 // 获得当前光标所在的列数  
  339.                 int column = cursor.getColumn();  
  340.                 // 当前光标所在单元格的值赋给文本框  
  341.                 text.setText(row.getText(column));  
  342.                 // 为文本框注册键盘事件  
  343.                 text.addKeyListener(new KeyListener()  
  344.                 {  
  345.                     @Override  
  346.                     public void keyPressed(KeyEvent e)  
  347.                     {  
  348.                         // 此时在文本框上单击回车后,这是表格中的数据为修改后文本框中的数据  
  349.                         // 然后释放文本框资源  
  350.                         if (e.character == SWT.CR)  
  351.                         {  
  352.                             TableItem row = cursor.getRow();  
  353.                             int column = cursor.getColumn();  
  354.                             row.setText(column, text.getText());  
  355.                             text.dispose();  
  356.                         }  
  357.                         // 如果在文本框中单击了ESC键,则并不对表格中的数据进行修改  
  358.                         if (e.character == SWT.ESC)  
  359.                         {  
  360.                             text.dispose();  
  361.                         }  
  362.                     }  
  363.   
  364.                     @Override  
  365.                     public void keyReleased(KeyEvent e)  
  366.                     {  
  367.                         // TODO Auto-generated method stub  
  368.   
  369.                     }  
  370.                 });  
  371.                 // 注册焦点事件  
  372.                 text.addFocusListener(new FocusListener()  
  373.                 {  
  374.                     // 当该文本框失去焦点时,释放文本框资源  
  375.                     public void focusLost(FocusEvent e)  
  376.                     {  
  377.                         text.dispose();  
  378.                     }  
  379.   
  380.                     @Override  
  381.                     public void focusGained(FocusEvent e)  
  382.                     {  
  383.                         // TODO Auto-generated method stub  
  384.   
  385.                     }  
  386.                 });  
  387.                 // 将该文本框绑定到可编辑的控件上  
  388.                 editor.setEditor(text);  
  389.                 // 设置文本框的焦点  
  390.                 text.setFocus();  
  391.             }  
  392.   
  393.             // 移动光标到一个单元格上所触发的事件  
  394.             public void widgetSelected(SelectionEvent e)  
  395.             {  
  396.                 table.setSelection(new TableItem[]{cursor.getRow()});  
  397.             }  
  398.         });  
  399.         cursor.addMouseListener(new MouseListener()  
  400.         {  
  401.   
  402.             @Override  
  403.             public void mouseDoubleClick(MouseEvent e)  
  404.             {  
  405.                 // TODO Auto-generated method stub  
  406.   
  407.             }  
  408.   
  409.             @Override  
  410.             public void mouseDown(MouseEvent e)  
  411.             {  
  412.                 if (e.button == 3)  
  413.                 { // 右键按下,显示右键菜单  
  414.                     menu.setVisible(true);  
  415.                 }  
  416.             }  
  417.   
  418.             @Override  
  419.             public void mouseUp(MouseEvent e)  
  420.             {  
  421.                 // TODO Auto-generated method stub  
  422.   
  423.             }  
  424.   
  425.         });  
  426.         // ******************************************************/  
  427.         // 重新布局表格  
  428.         for (int i = 0; i < tableHeader.length; i++)  
  429.         {  
  430.             table.getColumn(i).pack();  
  431.         }  
  432.         // /****************************************************  
  433.         // 为单元格注册选中事件  
  434.         table.addSelectionListener(new SelectionAdapter()  
  435.         {  
  436.             public void widgetSelected(SelectionEvent e)  
  437.             {  
  438.                 // 获得所有的行数  
  439.                 int total = table.getItemCount();  
  440.                 // 循环所有行  
  441.                 for (int i = 0; i < total; i++)  
  442.                 {  
  443.                     TableItem item = table.getItem(i);  
  444.                     // 如果该行为选中状态,改变背景色和前景色,否则颜色设置  
  445.                     if (table.isSelected(i))  
  446.                     {  
  447.                         item.setBackground(sShell.getDisplay().getSystemColor(  
  448.                                 SWT.COLOR_RED));  
  449.                         item.setForeground(sShell.getDisplay().getSystemColor(  
  450.                                 SWT.COLOR_WHITE));  
  451.                     }  
  452.                     else  
  453.                     {  
  454.                         item.setBackground(null);  
  455.                         item.setForeground(null);  
  456.                     }  
  457.                 }  
  458.             }  
  459.   
  460.         });  
  461.         // ******************************************************/  
  462.     }  
  463.   
  464.     public static void main(String[] args)  
  465.     {  
  466.         // 调用主窗口  
  467.         Display display = Display.getDefault();  
  468.         TableSample thisClass = new TableSample();  
  469.         thisClass.createSShell();  
  470.         thisClass.sShell.open();  
  471.         while (!thisClass.sShell.isDisposed())  
  472.         {  
  473.             if (!display.readAndDispatch())  
  474.                 display.sleep();  
  475.         }  
  476.   
  477.         display.dispose();  
  478.     }  
  479.   
  480.     // 创建主窗口  
  481.     private void createSShell()  
  482.     {  
  483.         sShell = new Shell();  
  484.         sShell.setText("表格窗口");  
  485.         sShell.setLayout(new FillLayout());  
  486.         createViewForm();  
  487.         createMenu();  
  488.         sShell.setSize(new org.eclipse.swt.graphics.Point(307218));  
  489.         sShell.pack();  
  490.     }  
  491.   
  492.     // 创建上下文菜单  
  493.     private void createMenu()  
  494.     {  
  495.         // 创建弹出式菜单  
  496.         menu = new Menu(sShell, SWT.POP_UP);  
  497.         // 设置该菜单为表格菜单  
  498.         table.setMenu(menu);  
  499.         // 创建删除菜单项  
  500.         MenuItem del = new MenuItem(menu, SWT.PUSH);  
  501.         del.setText("删除");  
  502.         del.setImage(new Image(toolBar.getDisplay(), "icons//delete.gif"));  
  503.         // 为删除菜单注册事件,当单击时,删除所选择的行  
  504.         del.addListener(SWT.Selection, new Listener()  
  505.         {  
  506.             public void handleEvent(Event event)  
  507.             {  
  508.                 // 此处需添加删除绑定Control的代码  
  509.                 table.remove(table.getSelectionIndices());  
  510.             }  
  511.         });  
  512.         // 创建查看菜单项  
  513.         MenuItem view = new MenuItem(menu, SWT.PUSH);  
  514.         view.setText("查看");  
  515.         view.setImage(new Image(toolBar.getDisplay(), "icons//scope.gif"));  
  516.         // 为查看菜单项注册事件,当单击时打印出所选的姓名  
  517.         view.addListener(SWT.Selection, new Listener()  
  518.         {  
  519.             public void handleEvent(Event event)  
  520.             {  
  521.                 TableItem[] items = table.getSelection();  
  522.                 for (int i = 0; i < items.length; i++)  
  523.                     System.out.print(items[i].getText());  
  524.             }  
  525.         });  
  526.   
  527.         table.setMenu(menu);  
  528.     }  
  529.   
  530. }  



[java] view plain copy
  1. /* 
  2.  * 文件名:StartConfigDemo.java 
  3.  * 版权:Copyright 1986-2012 Andy. All Rights Reserved.  
  4.  * 描述: StartConfigDemo.java 
  5.  * 修改人:Andy 
  6.  * 修改时间:2012-10-26 
  7.  * 修改内容:新增 
  8.  */  
  9. package org.wang.andy.swtjface.demo.table;  
  10.   
  11. import java.util.Hashtable;  
  12. import java.util.Random;  
  13.   
  14. import org.eclipse.swt.SWT;  
  15. import org.eclipse.swt.custom.TableEditor;  
  16. import org.eclipse.swt.events.ModifyEvent;  
  17. import org.eclipse.swt.events.ModifyListener;  
  18. import org.eclipse.swt.graphics.Color;  
  19. import org.eclipse.swt.graphics.Image;  
  20. import org.eclipse.swt.layout.FillLayout;  
  21. import org.eclipse.swt.layout.GridData;  
  22. import org.eclipse.swt.layout.GridLayout;  
  23. import org.eclipse.swt.widgets.Composite;  
  24. import org.eclipse.swt.widgets.Display;  
  25. import org.eclipse.swt.widgets.Event;  
  26. import org.eclipse.swt.widgets.Label;  
  27. import org.eclipse.swt.widgets.Listener;  
  28. import org.eclipse.swt.widgets.Shell;  
  29. import org.eclipse.swt.widgets.Table;  
  30. import org.eclipse.swt.widgets.TableColumn;  
  31. import org.eclipse.swt.widgets.TableItem;  
  32. import org.eclipse.swt.widgets.Text;  
  33. import org.eclipse.swt.widgets.ToolBar;  
  34. import org.eclipse.swt.widgets.ToolItem;  
  35.   
  36. /** 
  37.  *  
  38.  * <pre> 
  39.  * </pre> 
  40.  *  
  41.  * @author Andy 
  42.  */  
  43. public class StartConfigDemo  
  44. {  
  45.     private static Hashtable<TableItem, TableItemControls> tablecontrols = new Hashtable<TableItem, TableItemControls>();  
  46.   
  47.     public static void main(String[] args)  
  48.     {  
  49.         Display display = Display.getDefault();  
  50.         final Shell shell = new Shell(display);  
  51.   
  52.         shell.setText("StartConfigDemo");  
  53.         shell.setBounds(150150300300);  
  54.         shell.setLayout(new FillLayout());  
  55.   
  56.         Composite rootComposite = new Composite(shell, SWT.NONE);  
  57.         GridLayout configGridLayout = new GridLayout();  
  58.         configGridLayout.numColumns = 2;  
  59.         rootComposite.setLayout(configGridLayout);  
  60.         rootComposite.setBackground(new Color(Display.getCurrent(), 255255,  
  61.                 255));  
  62.   
  63.         ToolBar toolBar = new ToolBar(rootComposite, SWT.FLAT);  
  64.         final ToolItem addItem = new ToolItem(toolBar, SWT.PUSH);  
  65.         addItem.setImage(new Image(toolBar.getDisplay(), "icons//add.gif"));  
  66.         final ToolItem deleteItem = new ToolItem(toolBar, SWT.PUSH);  
  67.         deleteItem  
  68.                 .setImage(new Image(toolBar.getDisplay(), "icons//delete.gif"));  
  69.         final ToolItem upItem = new ToolItem(toolBar, SWT.PUSH);  
  70.         upItem.setImage(new Image(toolBar.getDisplay(), "icons//up.gif"));  
  71.         final ToolItem downItem = new ToolItem(toolBar, SWT.PUSH);  
  72.         downItem.setImage(new Image(toolBar.getDisplay(), "icons//down.gif"));  
  73.   
  74.         GridData barGridData = new GridData();  
  75.         barGridData.horizontalSpan = 2;  
  76.         barGridData.horizontalAlignment = SWT.END;  
  77.         toolBar.setLayoutData(barGridData);  
  78.   
  79.         Label inputLabel = new Label(rootComposite, SWT.NONE);  
  80.         inputLabel.setText("Input:");  
  81.         inputLabel  
  82.                 .setBackground(new Color(Display.getCurrent(), 255255255));  
  83.         GridData inGridData = new GridData();  
  84.         inGridData.horizontalAlignment = SWT.BEGINNING;  
  85.         inGridData.verticalAlignment = SWT.TOP;  
  86.         inputLabel.setLayoutData(inGridData);  
  87.   
  88.         // ViewForm viewForm = new ViewForm(rootComposite, SWT.NONE);  
  89.         // viewForm.setTopCenterSeparate(true);  
  90.         //  
  91.         // Composite tabComposite = new Composite(viewForm, SWT.NONE);  
  92.         // GridLayout gridLayout = new GridLayout();  
  93.         // gridLayout.numColumns = 1;  
  94.         // tabComposite.setLayout(gridLayout);  
  95.   
  96.         final Table table = new Table(rootComposite, SWT.MULTI  
  97.                 | SWT.FULL_SELECTION | SWT.BORDER);  
  98.         table.setHeaderVisible(true);  
  99.         table.setLinesVisible(true);  
  100.   
  101.         String[] tableHeader = {"Name""Type""Default"};  
  102.         for (int i = 0; i < tableHeader.length; i++)  
  103.         {  
  104.             TableColumn tableColumn = new TableColumn(table, SWT.NONE);  
  105.             tableColumn.setText(tableHeader[i]);  
  106.         }  
  107.   
  108.         TableItem item = new TableItem(table, SWT.NONE);  
  109.         item.setText(new String[]{"aaa""vvv""ccc"});  
  110.         item = new TableItem(table, SWT.NONE);  
  111.         item.setText(new String[]{"bbb""111""cc222"});  
  112.         item = new TableItem(table, SWT.NONE);  
  113.         item.setText(new String[]{"周五""女""567"""});  
  114.   
  115.         GridData gridData = new GridData();  
  116.         gridData.horizontalAlignment = SWT.FILL;  
  117.         gridData.grabExcessHorizontalSpace = true;  
  118.         gridData.grabExcessVerticalSpace = true;  
  119.         gridData.verticalAlignment = SWT.FILL;  
  120.   
  121.         table.setLayoutData(gridData);  
  122.   
  123.         // 重新布局表格  
  124.         for (int i = 0; i < tableHeader.length; i++)  
  125.         {  
  126.             table.getColumn(i).pack();  
  127.         }  
  128.   
  129.         // 添加可编辑的单元格  
  130.         // /******************************************************  
  131.         TableItem[] items = table.getItems();  
  132.         for (int i = 0; i < items.length; i++)  
  133.         {  
  134.             // 第一列设置,创建TableEditor对象  
  135.             final TableEditor editName = new TableEditor(table);  
  136.             // 创建一个文本框,用于输入文字  
  137.             final Text name = new Text(table, SWT.NONE);  
  138.             // 将文本框当前值,设置为表格中的值  
  139.             name.setText(items[i].getText(0));  
  140.             // 设置编辑单元格水平填充  
  141.             editName.grabHorizontal = true;  
  142.             // 关键方法,将编辑单元格与文本框绑定到表格的第一列  
  143.             editName.setEditor(name, items[i], 0);  
  144.             // 当文本框改变值时,注册文本框改变事件,该事件改变表格中的数据。  
  145.             // 否则即使改变的文本框的值,对表格中的数据也不会影响  
  146.             name.addModifyListener(new ModifyListener()  
  147.             {  
  148.                 public void modifyText(ModifyEvent e)  
  149.                 {  
  150.                     editName.getItem().setText(1, name.getText());  
  151.                 }  
  152.   
  153.             });  
  154.   
  155.             // 第一列设置,创建TableEditor对象  
  156.             final TableEditor editType = new TableEditor(table);  
  157.             // 创建一个文本框,用于输入文字  
  158.             final Text type = new Text(table, SWT.NONE);  
  159.             // 将文本框当前值,设置为表格中的值  
  160.             type.setText(items[i].getText(1));  
  161.             // 设置编辑单元格水平填充  
  162.             editType.grabHorizontal = true;  
  163.             // 关键方法,将编辑单元格与文本框绑定到表格的第一列  
  164.             editType.setEditor(type, items[i], 1);  
  165.             // 当文本框改变值时,注册文本框改变事件,该事件改变表格中的数据。  
  166.             // 否则即使改变的文本框的值,对表格中的数据也不会影响  
  167.             type.addModifyListener(new ModifyListener()  
  168.             {  
  169.                 public void modifyText(ModifyEvent e)  
  170.                 {  
  171.                     editType.getItem().setText(1, type.getText());  
  172.                 }  
  173.   
  174.             });  
  175.   
  176.             // 第一列设置,创建TableEditor对象  
  177.             final TableEditor editValue = new TableEditor(table);  
  178.             // 创建一个文本框,用于输入文字  
  179.             final Text value = new Text(table, SWT.NONE);  
  180.             // 将文本框当前值,设置为表格中的值  
  181.             value.setText(items[i].getText(2));  
  182.             // 设置编辑单元格水平填充  
  183.             editValue.grabHorizontal = true;  
  184.             // 关键方法,将编辑单元格与文本框绑定到表格的第一列  
  185.             editValue.setEditor(value, items[i], 2);  
  186.             // 当文本框改变值时,注册文本框改变事件,该事件改变表格中的数据。  
  187.             // 否则即使改变的文本框的值,对表格中的数据也不会影响  
  188.             value.addModifyListener(new ModifyListener()  
  189.             {  
  190.                 public void modifyText(ModifyEvent e)  
  191.                 {  
  192.                     editValue.getItem().setText(1, value.getText());  
  193.                 }  
  194.   
  195.             });  
  196.             // 保存TableItem与绑定Control的对应关系,删除TableItem时使用  
  197.             TableItemControls cons = new TableItemControls(name, type, value,  
  198.                     editName, editType, editValue);  
  199.             tablecontrols.put(items[i], cons);  
  200.         }  
  201.   
  202.         Label outputLabel = new Label(rootComposite, SWT.NONE);  
  203.         outputLabel.setText("Output Type:");  
  204.         outputLabel  
  205.                 .setBackground(new Color(Display.getCurrent(), 255255255));  
  206.         GridData outGridData = new GridData();  
  207.         inGridData.horizontalAlignment = SWT.BEGINNING;  
  208.         outputLabel.setLayoutData(outGridData);  
  209.   
  210.         Text outPut = new Text(rootComposite, SWT.BORDER);  
  211.         gridData.horizontalAlignment = SWT.FILL;  
  212.         // gridData.grabExcessHorizontalSpace = true;  
  213.         // gridData.grabExcessVerticalSpace = true;  
  214.   
  215.         outPut.setLayoutData(outGridData);  
  216.   
  217.         // 工具栏按钮事件处理  
  218.         Listener listener = new Listener()  
  219.         {  
  220.             @Override  
  221.             public void handleEvent(Event event)  
  222.             {  
  223.                 // 如果单击添加按钮,添加一行,在实际的项目实现中通常是接收输入的参数,案后添加  
  224.                 // 这里为了简单起见,添加固定的一条记录  
  225.                 if (event.widget == addItem)  
  226.                 {  
  227.                     TableItem item = new TableItem(table, SWT.NONE);  
  228.                     item.setText(new String[]{"郑六" + new Random().nextInt(15),  
  229.                             "女""568"});  
  230.                 }  
  231.                 // 如果单击删除按钮  
  232.                 else if (event.widget == deleteItem)  
  233.                 {  
  234.                     // 首先获得表格中所有的行  
  235.                     TableItem[] items = table.getItems();  
  236.                     // 循环所有行  
  237.                     for (int i = items.length - 1; i >= 0; i--)  
  238.                     {  
  239.                         // 如果该行没有被选中,继续循环  
  240.                         if (!items[i].getChecked())  
  241.                             continue;  
  242.                         // 否则选中,查找该表格中是否有该行  
  243.                         int index = table.indexOf(items[i]);  
  244.                         // 如果没有该行,继续循环  
  245.                         if (index < 0)  
  246.                             continue;  
  247.                         // 删除绑定的控件  
  248.                         TableItemControls cons = tablecontrols  
  249.                                 .get(items[index]);  
  250.                         if (cons != null)  
  251.                         {  
  252.                             cons.dispose();  
  253.                             tablecontrols.remove(items[index]);  
  254.                             System.out.println("dispose " + index);  
  255.                         }  
  256.                         // 如果有该行,删除该行  
  257.                         // items[index].dispose();  
  258.                         table.remove(index);  
  259.                         System.out.println("i=" + i + ", index=" + index);  
  260.                         System.out.println("行数:" + table.getItemCount());  
  261.                         // table.pack();  
  262.                     }  
  263.                 }  
  264.                 // 如果单击上移按钮  
  265.                 else if (event.widget == upItem)  
  266.                 {  
  267.                     int selectedRow = table.getSelectionIndex();  
  268.                     if (selectedRow > 0)  
  269.                         table.setSelection(selectedRow - 1);// 设置选中的行数  
  270.                 }  
  271.                 // 如果单击下移按钮  
  272.                 else if (event.widget == upItem)  
  273.                 {  
  274.                     int selectedRow = table.getSelectionIndex();  
  275.                     if (selectedRow > -1  
  276.                             && selectedRow < table.getItemCount() - 1)  
  277.                         table.setSelection(selectedRow + 1);// 设置选中的行数  
  278.                 }  
  279.             }  
  280.   
  281.         };  
  282.         // 为工具栏的按钮注册事件  
  283.         addItem.addListener(SWT.Selection, listener);  
  284.         deleteItem.addListener(SWT.Selection, listener);  
  285.         upItem.addListener(SWT.Selection, listener);  
  286.         downItem.addListener(SWT.Selection, listener);  
  287.   
  288.         shell.open();  
  289.         while (!shell.isDisposed())  
  290.         {  
  291.             if (!display.readAndDispatch())  
  292.             {  
  293.                 display.sleep();  
  294.             }  
  295.   
  296.         }  
  297.         display.dispose();  
  298.   
  299.     }  
  300. }  
  301.   
  302. class TableItemControls  
  303. {  
  304.     Text name;  
  305.   
  306.     Text type;  
  307.   
  308.     Text value;  
  309.   
  310.     TableEditor tableeditor;  
  311.   
  312.     TableEditor tableeditor2;  
  313.   
  314.     TableEditor tableeditor1;  
  315.   
  316.     public TableItemControls(Text name, Text type, Text value,  
  317.             TableEditor tableeditor, TableEditor tableeditor1,  
  318.             TableEditor tableeditor2)  
  319.     {  
  320.         // super();  
  321.         this.name = name;  
  322.         this.type = type;  
  323.         this.value = value;  
  324.         this.tableeditor = tableeditor;  
  325.         this.tableeditor1 = tableeditor1;  
  326.         this.tableeditor2 = tableeditor2;  
  327.     }  
  328.   
  329.     public void dispose()  
  330.     {  
  331.         name.dispose();  
  332.         value.dispose();  
  333.         value.dispose();  
  334.         tableeditor.dispose();  
  335.         tableeditor1.dispose();  
  336.         tableeditor2.dispose();  
  337.     }  
  338. }