eclipse RCP开发 table相关的操作

插件开发时,表是用的比较多的控件之一,但SWT提供的表只能提供最简单的表。

本文通过具体的例子,对于表的每一列加自定义控件以及对于表的一些删除,增加行操作进行说明。

表的单元格里加自定义控件在http://blog.csdn.net/jdzms23/article/details/7248444里有说明,

但用cellEditor实现时,有一点不爽的地方就是必须单击此单元格后,控件才会显示出来,不知这种方式能不能把控件一直显示,求高人指点。

这里主要用tableEditor方式。

  1. import java.util.ArrayList;  
  2. import java.util.Hashtable;  
  3. import java.util.List;  
  4.   
  5. import org.eclipse.jface.window.ApplicationWindow;  
  6. import org.eclipse.swt.SWT;  
  7. import org.eclipse.swt.custom.TableEditor;  
  8. import org.eclipse.swt.events.MouseEvent;  
  9. import org.eclipse.swt.events.MouseListener;  
  10. import org.eclipse.swt.graphics.Point;  
  11. import org.eclipse.swt.widgets.Combo;  
  12. import org.eclipse.swt.widgets.Composite;  
  13. import org.eclipse.swt.widgets.Control;  
  14. import org.eclipse.swt.widgets.Display;  
  15. import org.eclipse.swt.widgets.Shell;  
  16. import org.eclipse.swt.widgets.Group;  
  17. import org.eclipse.swt.widgets.Button;  
  18. import org.eclipse.swt.widgets.Table;  
  19. import org.eclipse.swt.widgets.TableColumn;  
  20. import org.eclipse.swt.widgets.TableItem;  
  21. import org.eclipse.swt.widgets.Text;  
  22.   
  23. public class TestTableView extends ApplicationWindow {  
  24.     private Table table;  
  25.   
  26.     private String[][] contents = {{"""张三""男""未婚"},{"""李四""女""已婚"},{"""王五""女""未婚"}};  
  27.       
  28.     private Hashtable<TableItem, TableItemControls> tableControls = new   
  29.             Hashtable<TableItem, TestTableView.TableItemControls>();  
  30.     /** 
  31.      * Create the application window. 
  32.      */  
  33.     public TestTableView() {  
  34.         super(null);  
  35.     }  
  36.   
  37.     /** 
  38.      * Create contents of the application window. 
  39.      * @param parent 
  40.      */  
  41.     @Override  
  42.     protected Control createContents(Composite parent) {  
  43.         Composite container = new Composite(parent, SWT.NONE);  
  44.         {  
  45.             Group group = new Group(container, SWT.NONE);  
  46.             group.setText("表");  
  47.             group.setBounds(1010481238);  
  48.               
  49.             table = new Table(group, SWT.BORDER | SWT.FULL_SELECTION | SWT.CHECK);  
  50.             table.setBounds(1020461208);  
  51.             table.setHeaderVisible(true);  
  52.             table.setLinesVisible(true);  
  53.             {  
  54.                 TableColumn tableColumn = new TableColumn(table, SWT.NONE);  
  55.                 tableColumn.setWidth(20);  
  56.             }  
  57.             {  
  58.                 TableColumn tableColumn = new TableColumn(table, SWT.NONE);  
  59.                 tableColumn.setWidth(104);  
  60.                 tableColumn.setText("名");  
  61.             }  
  62.             {  
  63.                 TableColumn tableColumn = new TableColumn(table, SWT.NONE);  
  64.                 tableColumn.setWidth(110);  
  65.                 tableColumn.setText("性别");    
  66.             }  
  67.             {  
  68.                 TableColumn tableColumn = new TableColumn(table, SWT.NONE);  
  69.                 tableColumn.setWidth(100);  
  70.                 tableColumn.setText("婚否");  
  71.             }  
  72.               
  73.               
  74.         }  
  75.           
  76.         for (String[] content:contents) {  
  77.             TableItem item = new TableItem(table, SWT.NONE);  
  78.             item.setText(content);  
  79.         }  
  80.         createCells(table);  
  81.         {  
  82.             Button button = new Button(container, SWT.PUSH);  
  83.             button.setBounds(497417222);  
  84.             button.setText("追加");  
  85.             button.addMouseListener(new MouseListener() {  
  86.                   
  87.                 @Override  
  88.                 public void mouseUp(MouseEvent arg0) {  
  89.                     TableItem item = new TableItem(table, SWT.NONE);  
  90.                     item.setText(contents[0]);  
  91.                     //创建一个item的控件  
  92.                     createOneItemCells(item);  
  93.                 }  
  94.                   
  95.                 @Override  
  96.                 public void mouseDown(MouseEvent arg0) {  
  97.                     // TODO Auto-generated method stub  
  98.                       
  99.                 }  
  100.                   
  101.                 @Override  
  102.                 public void mouseDoubleClick(MouseEvent arg0) {  
  103.                     // TODO Auto-generated method stub  
  104.                       
  105.                 }  
  106.             });  
  107.         }  
  108.         {  
  109.             Button button = new Button(container, SWT.PUSH);  
  110.             button.setBounds(497987222);  
  111.             button.setText("删除");  
  112.             button.addMouseListener(new MouseListener() {  
  113.                 @Override  
  114.                 public void mouseUp(MouseEvent arg0) {  
  115.                     //储存删除索引的list  
  116.                     List<Integer> indexs = new ArrayList<Integer>();  
  117.                     for (int i = 0; i < table.getItemCount(); i++) {  
  118.                         TableItem item = table.getItem(i);  
  119.                           
  120.                         if (item.getChecked()) {  
  121.                             TableItemControls controls = tableControls.get(item);  
  122.                             if (controls != null) {  
  123.                                 controls.dispose();  
  124.                                 tableControls.remove(item);  
  125.                             }  
  126.                             indexs.add(i);  
  127.                         }  
  128.                     }  
  129.                     int idx[] = new int[indexs.size()];  
  130.                     for(int i=0;i<indexs.size() ; i++){  
  131.                         idx[i] = indexs.get(i);  
  132.                         System.out.println("idx[i]" + idx[i]);  
  133.                     }  
  134.                       
  135.                     //删除对应index的表的item  
  136.                     table.remove(idx);  
  137.                     table.pack();  
  138.                     //表大小还原  
  139.                     table.setBounds(1020461208);  
  140.                 }  
  141.                   
  142.                 @Override  
  143.                 public void mouseDown(MouseEvent arg0) {  
  144.                     // TODO Auto-generated method stub  
  145.                       
  146.                 }  
  147.                   
  148.                 @Override  
  149.                 public void mouseDoubleClick(MouseEvent arg0) {  
  150.                     // TODO Auto-generated method stub  
  151.                       
  152.                 }  
  153.             });  
  154.         }  
  155.   
  156.         return container;  
  157.     }  
  158.       
  159.     private void createCells(Table table) {  
  160.         for (int i = 0; i < table.getItemCount(); i++) {  
  161.             createOneItemCells(table.getItem(i));  
  162.         }  
  163.     }  
  164.       
  165.     private void createOneItemCells(TableItem item) {  
  166.         TableEditor nameEditor = new TableEditor(table);  
  167.         //名字的控件  
  168.         Text name = new Text(table, SWT.NONE);  
  169.         name.setText(item.getText(NAME_INDEX));  
  170.         nameEditor.grabHorizontal = true;  
  171.         nameEditor.setEditor(name, item, NAME_INDEX);  
  172.         //性别的控件  
  173.         TableEditor sexTableEditor = new TableEditor(table);  
  174.         int sexIndex = findElement(SEXS, item.getText(SEX_INDEX));  
  175.         Combo sexCombo = new Combo(table, SWT.DROP_DOWN | SWT.READ_ONLY);  
  176.         for (String sex : SEXS) {  
  177.             sexCombo.add(sex);  
  178.   
  179.         }  
  180.         sexCombo.select(sexIndex);  
  181.         sexTableEditor.grabHorizontal = true;  
  182.         sexTableEditor.setEditor(sexCombo, item, SEX_INDEX);  
  183.         //婚否的控件  
  184.         TableEditor marryTableEditor = new TableEditor(table);  
  185.         Button marryButton = new Button(table, SWT.CHECK);  
  186.         if (item.getText(MARRY_INDEX).equals("已婚")) {  
  187.             marryButton.setSelection(true);  
  188.         } else {  
  189.             marryButton.setSelection(false);  
  190.         }  
  191.   
  192.         marryTableEditor.grabHorizontal = true;  
  193.         marryTableEditor.setEditor(marryButton, item, MARRY_INDEX);  
  194.           
  195.         TableItemControls tableItemControls = new TableItemControls(name, sexCombo, marryButton,   
  196.                 nameEditor, sexTableEditor, marryTableEditor);  
  197.           
  198.         tableControls.put(item, tableItemControls);  
  199.       
  200.     }  
  201.       
  202.     private int findElement(String[] elements, String target) {  
  203.         for (int i = 0; i < elements.length; i++) {  
  204.             if(elements[i].equals(target)) {  
  205.                 return i;  
  206.             }  
  207.         }  
  208.         return -1;  
  209.     }  
  210.   
  211.     /** 
  212.      * Launch the application. 
  213.      * @param args 
  214.      */  
  215.     public static void main(String args[]) {  
  216.         try {  
  217.             TestTableView window = new TestTableView();  
  218.             window.setBlockOnOpen(true);  
  219.             window.open();  
  220.             Display.getCurrent().dispose();  
  221.         } catch (Exception e) {  
  222.             e.printStackTrace();  
  223.         }  
  224.     }  
  225.       
  226.     class TableItemControls {  
  227.         Text nameText;  
  228.         Combo sexCombo;  
  229.         Button marryButton;  
  230.         TableEditor nameEditor;  
  231.         TableEditor sexEditor;  
  232.         TableEditor marryEditor;  
  233.           
  234.         public TableItemControls(Text nameText, Combo sexCombo, Button marryButton,  
  235.                 TableEditor nameEditor, TableEditor sexEditor, TableEditor marryEditor  
  236.                 ) {  
  237.             this.nameText = nameText;  
  238.             this.sexCombo = sexCombo;  
  239.             this.marryButton = marryButton;  
  240.             this.nameEditor = nameEditor;  
  241.             this.sexEditor = sexEditor;  
  242.             this.marryEditor = marryEditor;  
  243.         }  
  244.           
  245.         public void dispose() {  
  246.             nameText.dispose();  
  247.             sexCombo.dispose();  
  248.             marryButton.dispose();  
  249.         }  
  250.     }  
  251.   
  252.     /** 
  253.      * Configure the shell. 
  254.      * @param newShell 
  255.      */  
  256.     @Override  
  257.     protected void configureShell(Shell newShell) {  
  258.         super.configureShell(newShell);  
  259.         newShell.setText("表操作相关");  
  260.     }  
  261.   
  262.     /** 
  263.      * Return the initial size of the window. 
  264.      */  
  265.     @Override  
  266.     protected Point getInitialSize() {  
  267.         return new Point(604379);  
  268.     }  
  269.       
  270.     private static final int NAME_INDEX = 1;  
  271.     private static final int SEX_INDEX = 2;  
  272.     private static final int MARRY_INDEX = 3;  
  273.     private static final int CHANCE_INDEX = 4;  
  274.       
  275.     private static final String[] SEXS = {"男""女"};  
  276. }  

运行效果如下图:
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值