2009-12-16 34 views

回答

7

在下面的链接帮助中做了TableEditor片段吗?

SWT Snippets

TableEditor部分的第一个示例使用上表中的SelectionListener(不像使用你提到你不想MouseDown事件的第二个例子)

也许你可以利用TraverseListenerKeyListener也可以帮助你实现你想要的。

4
final int EDITABLECOLUMN = 1; 
tblProvisionInfo.addSelectionListener(new SelectionAdapter() { 
     @Override 
     public void widgetSelected(SelectionEvent e) { 
      // Clean up any previous editor control 
      final TableEditor editor = new TableEditor(tblProvisionInfo);    
      // The editor must have the same size as the cell and must 
      // not be any smaller than 50 pixels. 
      editor.horizontalAlignment = SWT.LEFT; 
      editor.grabHorizontal = true; 
      editor.minimumWidth = 50; 
      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(tblProvisionInfo, 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);  

     } 
    });  

这里tblProvision是你的表的名字。你现在可以点击它来编辑你的表格。我已申报EDITABLECOLUMN。这是你想编辑的column

+0

在这种情况下,只有一列可以编辑。它是否正确? – Zoot 2013-09-30 21:11:26

0

您可以获取或设置项的值,例如:

Table table = new Table(parent, SWT.NONE); 
TableItem item = new TableItem(table, SWT.NONE); 
item.setText("My new Text"); 
0

我建议你给我们TableViewer,它是它,你可以使用数据绑定很容易得非常强大的表格。