Eclipse4: Unable to access STW widget of a PART if

2019-09-04 23:30发布

问题:

So I declared my own part like that:

import javax.annotation.PostConstruct;
import javax.inject.Inject;

import org.eclipse.e4.ui.di.Focus;
import org.eclipse.swt.SWT;
import org.eclipse.swt.browser.Browser;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.Composite;

public class MyPart {

    private Browser browser;

    @Inject
    public MyPart() {
    }

    @PostConstruct
    public void createComposite(Composite parent) {
        parent.setLayout(new FillLayout());
        Browser browser = new Browser(parent, SWT.NONE);
    }

    @Focus
    public void onFocus() {
        if (browser!= null){
            browser.forceFocus();
        }
    }

    public Browser getBrowser() {
        return browser;
    }

}

From another point in my application I get a reference to this PART via

    @Inject private EPartService partService;
    MPart clientPart = partService.findPart("rcp.parts.clientpart");
    MyPart view = (MyPart)clientPart.getObject();

I double checked that the reference retrieved and the part shown in the application have the same object ID so they are the same. BUT if I call getBrowser() I always get a null object. I tried the same scenario with a String and this worked.

Is the problem that it is a SWT widget?

回答1:

Your createComposite is assigning to a local variable:

Browser browser = new Browser(parent, SWT.NONE);

not the class member - should be

browser = new Browser(parent, SWT.NONE);


标签: eclipse-rcp