Java EclipseRCP获取部件实例

Java EclipseRCP获取部件实例,java,eclipse,reference,rcp,Java,Eclipse,Reference,Rcp,我试图获得对连接到Java类的部分的引用。我可以用 `@PostConstruct public void createComposite(Composite parent) { }` 然后“parent”变量就是我需要的。但我想有另一种方法。我正在尝试添加静态变量以保存它,如下所示: public class BibliotekaZmianyPart { private static Label label; private static Button button; private st

我试图获得对连接到Java类的部分的引用。我可以用

`@PostConstruct
public void createComposite(Composite parent) {

}`
然后“parent”变量就是我需要的。但我想有另一种方法。我正在尝试添加静态变量以保存它,如下所示:

public class BibliotekaZmianyPart {
private static Label label;
private static Button button;
private static Composite part;

@PostConstruct
public void createComposite(Composite parent) {
    part = parent;
}

public static void editBook() {
    GridLayout layout = new GridLayout(2, false);
    part.setLayout(layout);
    label = new Label(part, SWT.NONE);
    label.setText("A label");
    button = new Button(part, SWT.PUSH);
    button.setText("Press Me");
}}

然后“part”应该是我需要的变量,但它不起作用

不能有这样一个引用实例变量的静态方法

如果要引用另一个零件中的现有零件,请使用
EPartService
查找该零件:

@Inject
EPartService partService;


MPart mpart = partService.findPart("part id");

BibliotekaZmianyPart part = (BibliotekaZmianyPart)mpart.getObject();

part.editBook();   // Using non-static 'editBook'
如果零件尚未打开,则使用零件服务
showPart
方法:

MPart mpart = partService.showPart("part id", PartState.ACTIVATE);

BibliotekaZmianyPart part = (BibliotekaZmianyPart)mpart.getObject();

part.editBook();
所以你的课程是:

public class BibliotekaZmianyPart {
private Label label;
private Button button;
private Composite part;

@PostConstruct
public void createComposite(Composite parent) {
    part = parent;
}

public void editBook() {
    GridLayout layout = new GridLayout(2, false);
    part.setLayout(layout);
    label = new Label(part, SWT.NONE);
    label.setText("A label");
    button = new Button(part, SWT.PUSH);
    button.setText("Press Me");

    // You probably need to call layout on the part
    part.layout();
}}

它不起作用。我需要“Composite”变量在editBook()方法中创建标签。如何在另一种方法中获得对“复合父对象”的引用。您仍然像以前一样将复合对象保存在构造函数中,并使用非静态的
editBook
方法(也不要对变量使用
static
)。此代码假定在您要调用
editBook
时已创建零件。现在我在调用(现在是非静态的)editBook()方法时使用您的代码,但它仍然不会创建新标签。但当我将创建标签的代码复制到“createComposite”方法时,标签会在应用程序启动后立即创建。我认为问题在于“part=parent”行。是否包含part.layout()调用?由于您是在复合材料显示后添加到复合材料中的,因此需要告诉它更新眼睛,现在它可以工作了。非常非常非常感谢你。