我试图获取对连接到Java类的部分的引用。我可以使用
`@PostConstruct
public void createComposite(Composite 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”应该是我需要的变量-但它不起作用。
答案 0 :(得分:0)
您不能拥有引用实例变量的静态方法。
如果要引用另一个零件中的现有零件,请使用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();
}}