如何将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;
}
}
}
}
你能帮我解决这个问题吗?我真的不能在一段时间内解决它......
答案 0 :(得分:0)
这真是一个java问题!
但解决方法是添加最终关键字....
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);