简体   繁体   English

如何将JavaFX与Java Swing应用程序集成?

[英]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. 我想创建一个扩展JFXPanel以处理折线图的类,并将此JFXPanel与我在Java SE中创建的应用程序集成,我还将数据从我的JFrame应用程序实时传递到图形。

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. 错误消息告诉您必须运行FX应用程序线程中涉及JavaFX的所有内容,因为JavaFX不是线程安全的。

In order to execute something on that thread you use Platform.runLater 要在该线程上执行某些操作,请使用Platform.runLater

Note that there is a similar restriction for Swing, with the difference, that nothing will throw an Exception. 请注意,Swing有一个类似的限制,区别在于没有任何东西会抛出异常。 Instead you'll just get weird behavior sooner or later. 相反,你迟早会得到奇怪的行为。 For SwingUtilities.invokeLater 对于SwingUtilities.invokeLater

The Scene object and the call the JFXPanel.setScene need to occur on the JavaFX application thread like so: Scene对象和JFXPanel.setScene的调用需要在JavaFX应用程序线程上发生,如下所示:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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