可以。使用jface中的TableViewer,可以为每一column设置CellEditor。要使用 celleditor起作用还要调用treeviewer的 setCellModifier方法,实现ICellModifier的接口.

解决方案 »

  1.   

    可以在TableEditor中加入text、label,button等等
    这个例子是在table中放text
    package Nbit.actions;import org.eclipse.swt.*;
    import org.eclipse.swt.custom.*;
    import org.eclipse.swt.events.*;
    import org.eclipse.swt.layout.*;
    import org.eclipse.swt.widgets.*;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,"Name");
        item1.setText(1,"Naoki Takezoe");
        
        TableItem item2 = new TableItem(table,SWT.NULL);
        item2.setText(0,"E-Mail");
        item2.setText(1,"[email protected]");    final TableEditor tableEditor = new TableEditor(table);
        tableEditor.grabHorizontal = true;
            table.addSelectionListener(new SelectionAdapter() {      private static final int EDIT_COLUMN = 1;
          
          public void widgetSelected(SelectionEvent e){
            int index = table.getSelectionIndex();
            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 ();
      }
    }
      

  2.   

    SWT本身带的TABLE功能较弱,一般的显示用还可以,如果还使用在数据交互比较复杂的应用中还是不能尽人意的,我花了半年时间写了个类似EXCEL表格的控件,现打算提供成开源项目形式开放出来,需要代码的可以联系我[email protected]
      

  3.   

    yaobifeng(yaobifeng) :我个人觉得只要把table中的每一个cell可以添加一个object就可以添加各个控件了,我没有实际做过,也不知道会出现什么问题,我想这样应该可以实现的,因为在java里面,所有的类都是object类的子类,而继承的关系又可以向上兼容的。
      

  4.   

    你的想法不错,实际上在表格类控件的开发过程中的确是用到这种思路的,在设计模式里叫:适配器,简单的说可以为单元格设计一个适配接口,其实就是在单元格类中保留一个对象引用,在SWT中,所有的控件都是从Control 类中继承的,当然Control也是object的一个子类,只是Control提供了大部分事件的支持,及一些控件应该有的方法,如setfocus,enable等等。SWT自带的Table好像是没提供CellRenderer(没深入研究过),这也是我自己表格控件的一个理由,呵呵,网上也已经有一个开源的SWT表格控件,功能还行,有CellRenderer,支持从控件外部往单元格中放入控件的功能,一般的应用也能满足了,不过没有打印预览等支持,这又是我自己做一个新的理由,呵呵