java - Eclipse RCP 获取部件实例

标签 java eclipse reference rcp

我正在尝试获取与 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");
}}

然后“部分”应该是我需要的变量 - 但它不起作用。

最佳答案

不能有像引用实例变量那样的静态方法。

如果您想从另一个部件引用现有部件,请使用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();

所以你的类(class)将是:

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();
}}

关于java - Eclipse RCP 获取部件实例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51233649/

相关文章:

java - 随机变量导致的运行时错误

eclipse - 如何更改 Eclipse 自动生成代码的缩进样式?

c++ - 对取消引用的 NULL 指针的引用是否会在 C++ 中创建或访问时产生 UB

rust - 我无法理解 Rust "scope"定义(Rust 编程语言,第二版。Klabnik & Nichols)

java - JPA 2.0/EclipseLink 和 sqlite 3.7.2 - 无法创建表

java - Gluon JavaFX 中有文件导航吗?

Java 变量可见性

java - JNDI DB连接可以存储在Session中吗?

带有 GSON 的 Java 堆空间

Python:更新引用值