简体   繁体   中英

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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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