如何在 e4 SWT 中显示 JavaFX 图表?

How can I show JavaFX Charts in e4 SWT?

如何在 e4 中显示 JavaFX 图表?

比如这个。

 Scene scene = new Scene(new Group());
        stage.setTitle("Imported Fruits");
        stage.setWidth(500);
        stage.setHeight(500);

        ObservableList<PieChart.Data> pieChartData =
                FXCollections.observableArrayList(
                new PieChart.Data("Grapefruit", 13),
                new PieChart.Data("Oranges", 25),
                new PieChart.Data("Plums", 10),
                new PieChart.Data("Pears", 22),
                new PieChart.Data("Apples", 30));
        final PieChart chart = new PieChart(pieChartData);
        chart.setTitle("Imported Fruits");

        ((Group) scene.getRoot()).getChildren().add(chart);
        stage.setScene(scene);
        stage.show();

这里有什么我可以Inject的吗?

已更新

代码

@PostConstruct
    public void initializeChart(Composite parent) {
        FXCanvas canvas = new FXCanvas(parent, SWT.NONE);
        Group group = new Group();
        Scene scene = new Scene(group);
        ObservableList<PieChart.Data> pieChartData = FXCollections
                .observableArrayList(new PieChart.Data("Grapefruit", 13),
                        new PieChart.Data("Oranges", 25), new PieChart.Data(
                                "Plums", 10), new PieChart.Data("Pears", 22),
                        new PieChart.Data("Apples", 30));
        final PieChart chart = new PieChart(pieChartData);
        chart.setTitle("Imported Fruits");
        group.getChildren().add(chart);
        canvas.setScene(scene);
    }

我使用 jdk 1.8.0_45 启动我的应用程序,但我收到以下错误

java.lang.NoClassDefFoundError: javafx/scene/Parent
    at java.lang.Class.getDeclaredConstructors0(Native Method)
    at java.lang.Class.privateGetDeclaredConstructors(Class.java:2671)
    at java.lang.Class.getDeclaredConstructors(Class.java:2020)
    at org.eclipse.e4.core.internal.di.InjectorImpl.internalMake(InjectorImpl.java:306)
    ... 124 more
Caused by: java.lang.ClassNotFoundException: javafx.scene.Parent cannot be found by amap.impactassessment_1.0.0.qualifier
    at org.eclipse.osgi.internal.loader.BundleLoader.findClassInternal(BundleLoader.java:423)
    at org.eclipse.osgi.internal.loader.BundleLoader.findClass(BundleLoader.java:336)
    at org.eclipse.osgi.internal.loader.BundleLoader.findClass(BundleLoader.java:328)
    at org.eclipse.osgi.internal.loader.ModuleClassLoader.loadClass(ModuleClassLoader.java:160)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
    ... 128 more

您的问题与该问题重复 e(fx)clipse equinox compilation error 但自那以后有些事情发生了变化,我会在这里回答。

首先,我引用 Tom Schindl 对原始问题的回答:

The reason you see the "java.lang.NoClassDefFoundError" is that Equinox does not consult the Ext-classpath and most likely you don't tell Equinox to use our adaptor hooks who make equinox consult that path.

What you need to do is to:

Ensure that you have org.eclipse.fx.osgi fragment in your launch and make sure you pass -Dosgi.framework.extensions=org.eclipse.fx.osgi as VM arguments in your launch config

使用此方法,您还必须在 MANIFEST.MF 中导入 javafx.* 包,并且您需要在目标平台中安装包 org.eclipse.fx.javafx 以提供这些导入.

另一种更简单的方法是通过添加

告诉 Equinox 使用扩展 class 加载程序
-Dorg.osgi.framework.bundle.parent=ext

到 vm 参数。使用它,您不再 需要捆绑包 org.eclipse.fx.osgiorg.eclipse.fx.javafx,并且您不需要修改 MANIFEST.MF

这种风格最近得到了 Tom Schindl 的建议,也被 2.0 版的 e(fx)clipse 使用。有关详细信息,请参阅此论坛 post:https://www.eclipse.org/forums/index.php/t/1063826/