0

如何将 text.getText() 传递给 selectionChanged ?铸造似乎不起作用。我在按钮按下时使用它。这是完整的课程,但是 StackOverflow 不允许我在不逐行解释的情况下在这里放更多文本。

Listener listener = new Listener() {
    public void handleEvent(Event event) {
        if (event.widget == button3) {
            viewer.setSelection(text.getText());
        }
    }


public class OpisView extends ViewPart implements ActionListener,ISelectionListener {

    public final static String VIEW_ID="DetailsView";
    private String path;
    public Composite x ;
    private TableViewer viewer;
            //public static final String VIEW_ID = "com.example.rcpmvc.calculator";

            @Override
            public void createPartControl(final Composite parent) {
                final Text text = new Text(parent, SWT.NONE);

                getViewSite().getPage().addSelectionListener(this);
                viewer = new TableViewer(parent, SWT.MULTI | SWT.H_SCROLL | SWT.V_SCROLL);
                PlatformUI.getWorkbench().getHelpSystem().setHelp(viewer.getControl(), "Widoki.OpisView");
                text.setText("");
                x = parent;

                parent.setLayout(new GridLayout(2, false));
                GridData gridData = new GridData();
                gridData.widthHint = 50;
                gridData.heightHint = 30;
                getSite().setSelectionProvider(viewer);
                getViewSite().getPage().addSelectionListener(this);


                final Button button1 = new Button(parent, SWT.PUSH);
                final Button button2 = new Button(parent, SWT.PUSH);
                final Button button3 = new Button(parent, SWT.PUSH);

                Listener listener = new Listener() {
                      public void handleEvent(Event event) {
                        if (event.widget == button1) {

                            FileRead x = new FileRead();
                            try {
                                x.Add(text.getText(),path);
                                showMessage("Pomyslnie otagowano " +  path + ", tagiem " + text.getText());
                            } catch (IOException e) {
                                // TODO Auto-generated catch block
                                e.printStackTrace();
                            }

                        } 
                        if (event.widget == button2) {


                            FileRead x = new FileRead();
                            try {
                                x.Remove(text.getText(),path);
                                showMessage("Pomyslnie usunieto tag " + text.getText() + " z pliku " + path);
                            } catch (IOException e) {
                                // TODO Auto-generated catch block
                                e.printStackTrace();
                            }

                        }
                        if (event.widget == button3) {

                                text.getText();


                        }
                      }
                    };

                text.addListener(SWT.KeyDown, listener);
                button1.setLayoutData(gridData);
                button1.addListener(SWT.Selection, listener);
                button1.setText("Dodaj");


                button2.setLayoutData(gridData);
                button2.addListener(SWT.Selection, listener);
                button2.setText("Usuń");

                button3.setLayoutData(gridData);
                button3.addListener(SWT.Selection, listener);
                button3.setText("Wyszukaj");

                // Set the sorter for the table

                //sGridLayoutFactory.fillDefaults().numColumns(3).spacing(3, 0).margins(0, 0).applyTo(parent);

                GridLayoutFactory.swtDefaults().numColumns(3).spacing(0, 0).margins(0, 0).applyTo(parent);

            }

            private void showMessage(String message) {
                MessageDialog.openInformation(
                    x.getShell(),
                    "Opis",
                    message);
            }

            public void selectionChanged(IWorkbenchPart part, ISelection selection) {
                if (selection instanceof IStructuredSelection) {
                    Object obj = ((IStructuredSelection) selection).getFirstElement();

                    if (obj instanceof String) {

                        path = (String) obj;
                    }
                }   
            }

}

你能帮我解决这个问题吗?我真的有一段时间不能整理了...

4

1 回答 1

0

这真的是一个问题!

但解决方案是添加 final 关键字....

final Text text = new Text(parent, SWT.NONE);
Listener listener = new Listener() {
   public void handleEvent(Event event) {
      if (event.button == SWT.BUTTON3) {
         viewer.setSelection(text.getText());
      }
   }
};
text.addListener(SWT.KeyDown, listener);
于 2012-06-12T15:53:28.190 回答