必须单击下一个文本小组件以保存以前的文本内容

时间:2014-04-28 12:09:57

标签: java text swt

我使用SWT创建一个表,在这个表的第四列需要是可编辑的,所以我用TableEditor来创建它。但是每次我需要单击下一个Widget Text来保存以前文本内容的内容。例如:在我的表中,第四列可以编辑, 我在第一行第四列填写数字,我需要点击第二行第四列,只有这样才能保存第一行第四行文本的内容。为什么?跟着是我的 代码:

final TableEditor editor=new TableEditor(table);
       editor.horizontalAlignment=SWT.LEFT;
       editor.grabHorizontal=true;
       final int EDITABLECOLUMN=3;

       table.addSelectionListener(new SelectionAdapter() {
              public void widgetSelected(SelectionEvent e) {
                // Clean up any previous editor control
                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(table, 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);
              }
            });

和我的表以这种方式创建:

public Table createTable(Composite parent){
        //表格布局
        GridData gridData=new GridData();
        gridData.horizontalAlignment=SWT.FILL;
        gridData.grabExcessHorizontalSpace=true;
        gridData.grabExcessVerticalSpace=true;
        gridData.verticalAlignment=SWT.FILL;

        //创建表格,及其格式
       final Table table=new Table(parent,SWT.CHECK|SWT.MULTI|SWT.FULL_SELECTION|SWT.V_SCROLL|SWT.H_SCROLL);
       table.setHeaderVisible(true);
       table.setLayoutData(gridData);
       table.setLinesVisible(true);

       //创建表头
       String[] tableHeader={"Num","Package","Class","Run Times"};
       int[] tableColumnWidth={100,150,400,80};
       for(int i=0;i<tableHeader.length;i++){
           TableColumn tableColumn=new TableColumn(table,SWT.NONE|SWT.CENTER);
           tableColumn.setText(tableHeader[i]);            
           tableColumn.setMoveable(true);
           tableColumn.setWidth(tableColumnWidth[i]);
       }
        table.addListener(SWT.Selection, tableListener);              
    return table;
}

我更改了标记为第一的区域内容,此内容仅在我点击区域2时保存。这意味着只需单击区域2即可激活该事件: I changed the content of area where was marked as number one, this content only saved when I clicked area 2. It means only clicking area 2 to active the event

0 个答案:

没有答案