How to Use JavaFX in Eclipse Swing Project

2019-08-06 06:29发布

问题:

I'm wanting to use a few JavaFX components in a Swing app (notably JFXPanel for its HTML 5 rendering support). My app is currently set up in Eclipse 4.3 (Kepler) and I have the latest Java 7 JDK installed on my machine.

This tutorial from Oracle suggests that all you have to do is just refer to a JavaFX class and it'll work in a Swing app. So I type in JFXPanel panel = new JFXPanel(), but Eclipse does not recognize it as a valid Java class. It does recognize other Java 7 classes, like java.nio.

I can't figure out how to get Eclipse to recognize JavaFX classes. This question was previously answered about JavaFX in Eclipse. The first response said that you need to start by creating a new JavaFX project, which I can't do because I already have a fairly developed Swing app. The second is to use the e(fx)clipse plugin, but the tutorial also seems to require this. If there's something special I have to do in order to get a non-JavaFX project in Eclipse to use JavaFX classes, I can't figure out how to do it.

回答1:

The problem with JavaFX is that it is not on the default classpath so eclipse won't find it (and your exported swing application won't either unless you use the oracle packaging utility!).

So you have 2 options:

a) you install e(fx)clipse and modify the projects Buildpath to include the JavaFX-SDK library.

b) you add the jfxrt.jar yourself to the projects Buildpath (you don't need anything else).

e(fx)clipse would give you extra support for FX development. The version that can be installed into Kepler can be found at http://download.eclipse.org/efxclipse/updates-nightly/site