简体   繁体   中英

How to integrate JavaFX with Java Swing application?

I want to create a class that extends JFXPanel to handle a line chart, and integrate this JFXPanel with my application made ​​in Java SE, I also pass data to the graph in real-time from my JFrame application.

Here is my example code:

    public class LineChartJFXPanel extends JFXPanel {

        private final LineChart<Number,Number> chart;
        private final HashMap<String,  XYChart.Series<Number,Number>> series;
        private final NumberAxis xAxis, yAxis;

        public LineChartJFXPanel() {
            Platform.setImplicitExit(false);
            series = new HashMap<>();
            xAxis = new NumberAxis(0.0,10.0,1.0);
            yAxis = new NumberAxis(0.0,10.0,1.0);
            chart = new LineChart<>(xAxis,yAxis);
            setScene(new Scene(chart));
        }

        public void setSeries(String idSeries, String nameSeries, Number xIni, Number yIni){
            XYChart.Series<Number,Number> newSeries = new XYChart.Series<>();
            newSeries.setName(nameSeries);
            newSeries.getData().add(new XYChart.Data<>(xIni,yIni));
            chart.getData().add(newSeries);
            series.put(idSeries, newSeries);
        }
    }

******************************************

    public class PanelChart extends JPanel{

        LineChartJFXPanel lineChart;

        public PanelChart(){
            setLayout(new GridLayout(1, 2));
            lineChart =  new LineChartJFXPanel();
            add(lineChart);
        }
    }


********************************

    public class Main extends JFrame{
        PanelChart chartPanel;
        public Main(){
            setPreferredSize(new Dimension(800, 600));
            chartPanel = new PanelChart();
            getContentPane().add(chartPanel);
            setVisible(true);
        }

        public static void main(String args[]){
            Main main = new Main();
            main.setVisible(true);
        }

    }

But when I run the code, it shows me the following error:

Exception in thread "main" java.lang.IllegalStateException: Not on FX application thread; currentThread = main

at com.sun.javafx.tk.Toolkit.checkFxUserThread(Toolkit.java:237)
at com.sun.javafx.tk.quantum.QuantumToolkit.checkFxUserThread(QuantumToolkit.java:400)
at javafx.scene.Scene.<init>(Scene.java:290)
at javafx.scene.Scene.<init>(Scene.java:198)
at javachartdemo.LineChartJFXPanel.<init>(LineChartJFXPanel.java:39)
at javachartdemo.PanelChart.<init>(PanelChart.java:22)
at javachartdemo.Main.<init>(Main.java:21)
at javachartdemo.Main.main(Main.java:27)

What do I need to change in my application?

The error message tells you that you have to run everything that touches JavaFX in the FX application thread, since JavaFX isn't thread safe.

In order to execute something on that thread you use Platform.runLater

Note that there is a similar restriction for Swing, with the difference, that nothing will throw an Exception. Instead you'll just get weird behavior sooner or later. For SwingUtilities.invokeLater

The Scene object and the call the JFXPanel.setScene need to occur on the JavaFX application thread like so:

Platform.runLater(() -> {
  setScene(new Scene(chart));
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM