在Java SWT中的组合中修改文本

时间:2018-08-16 07:52:29

标签: java swt

要选择文件并保存文件路径,我具有以下代码:

case FILE :
      final Composite fileBaseComposite = new Composite(table, SWT.BORDER);
      fileBaseComposite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false));
      final GridLayout fileBaseCompositeGridLayout = new GridLayout(2, false);
      fileBaseCompositeGridLayout.marginHeight = 0;
      fileBaseCompositeGridLayout.marginWidth = 0;
      fileBaseComposite.setLayout(fileBaseCompositeGridLayout);

      final Text selectFiletext = new Text(fileBaseComposite, SWT.SINGLE);
      selectFiletext.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
      selectFiletext.setText(aCurrentContent);
      selectFiletext.addModifyListener(new ModifyListener() {
        @Override
        public void modifyText(ModifyEvent e)
        {
          Text text = (Text)tableEditor.getEditor();
          tableEditor.getItem().setText(ARGUMENT_VALUE_COLUMN, text.getText());
        }
      });

      final Button selectFileButton = new Button(fileBaseComposite, SWT.NONE);
      selectFileButton.setText("Browse");
    selectFileButton.setLayoutData(new GridData(SWT.FILL, SWT.FILL, false, true));
    selectFileButton.addSelectionListener(new SelectionAdapter() {
      @Override
      public void widgetSelected(SelectionEvent e)
      {
        FileDialog fileSelectDialog = new FileDialog(fileBaseComposite.getShell(), SWT.OPEN);
        fileSelectDialog.setText("Select File");

        // String is saved separately because otherwise it opens twice
        String filePath = fileSelectDialog.open();
        if (filePath != null) {
          selectFiletext.setText(filePath);
          tableEditor.getItem().setText(ARGUMENT_VALUE_COLUMN, filePath);
        }
      }
    });
    return fileBaseComposite;

当我尝试手动修改文本时,出现以下错误消息问题:

java.lang.ClassCastException: org.eclipse.swt.widgets.Composite cannot be cast to org.eclipse.swt.widgets.Text

我了解此问题,但找不到解决方案。是否有可能将文本放入复合材料中?

我希望在一个表格单元格中有一个文本和一个按钮。文本应可手动编辑并可读以确保安全。

1 个答案:

答案 0 :(得分:0)

selectDirectoryText.addModifyListener(new ModifyListener() {
      @Override
      public void modifyText(ModifyEvent e)
      {
        Composite comp = (Composite)tableEditor.getEditor();
        Text text = (Text)comp.getChildren()[0];
        tableEditor.getItem().setText(ARGUMENT_VALUE_COLUMN, text.getText());
      }
    });

多数民众赞成在解决方案.. 我想将Composite直接投射到Text