Java – Using JavaFX Chart in Swing Application

javajavafxjfreechartswing

I have a traditional Java swing application (extends JFrame and has a main class) that uses JFreeCharts for some charting functionality. I have recently seen JavaFX and think these charts look alot refresher and will give my users a better experience. I wish to embed a JavaFX chart scene into a jInternalFrame (which in turn is called from my jDesktopPane).

I have followed a simple tutorial on how to create both a javafx application and a javafx chart but I am stuck on to how I get the scene inside my existing code. One example I have been through shows me how to achieve this but means converting my project to a javafx one which extends "application" class.

How can I achieve what I want? Is it not possible to keep my existing JFrame as the top level class and simply add a JFXPanel to my jinternalframe.

Any help would be great, please note I have been through some tutorials and they require me to convert my project to javafx – I simply want a javafx scene in my existing swing app.

Many thanks,

Best Answer

You don't need to convert your Swing application to a JavaFX application. You can place a JavaFX Chart in a JFXPanel which may be placed in a Swing component. The javadoc I linked includes sample code for embedding a JavaFX node in a Swing application.

Review the official JavaFX for Swing Developers tutorial trail from Oracle.

Also look at the Embedding JavaFX in Swing sample of SwingInterop for embedding a chart in a Swing application. The sample is provided by Oracle under the BSD open source license so that you can use it in your application.

Despite all this, my advice, unless it is a large, existing Swing code base that you just want to use a couple of JavaFX features in, is to write your application as a pure JavaFX application rather than a mixed Swing/JavaFX application.

Related Topic