简体   繁体   English

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

[英]Modify a Text inside a composite in Java SWT

To choose a file and save the filepath I have the following code: 要选择一个文件并保存文件路径,我需要以下代码:

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;

when I try to modify the text manually, I've got a problem with the following error message: 当我尝试手动修改文本时,以下错误消息出现了问题:

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

I understand the issue but cant find a solution. 我了解这个问题,但找不到解决方案。 Is there a possibility to get the text inside the composite? 是否有可能将文本放入复合材料中?

I want a text and a button in one table cell. 我想要一个表单元格中的文本和按钮。 The text should be editable manually and readable to safe it. 文本应可手动编辑并可读以确保安全。

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());
      }
    });

thats the solution.. I wanted to cast the Composite directly to a Text . 多数民众赞成在解决方案..我想直接将Composite转换为Text

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM