Display problem when adding Swing node in JavaFX application

231 views Asked by At

I have a JavaFX application for plotting a heatmap. The app is developed with Oracle's JDK 8 on a Windows 10 machine. I'm using xchart-3.8.0.jar library (https://knowm.org/downloads/xchart/xchart-3.8.0.zip) for plotting the data through a swing node.

Here is the code:

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.stage.Stage;
import javafx.scene.layout.VBox;
import javafx.scene.layout.Priority;
import javafx.embed.swing.SwingNode;

import org.knowm.xchart.HeatMapChart;
import org.knowm.xchart.HeatMapChartBuilder;
import org.knowm.xchart.XChartPanel;
import org.knowm.xchart.style.Styler;

import javax.swing.*;
import java.awt.*;
import java.io.IOException;
import java.util.Arrays;
import java.util.Collections;

public class XChartExample extends Application {

    @Override public void start(Stage stage) {
        VBox vBox = new VBox();

        stage.setTitle("Heatmap");
    
        HeatMapChart chart =
            new HeatMapChartBuilder().xAxisTitle("X").yAxisTitle("Y").theme(Styler.ChartTheme.Matlab).build();

        chart.getStyler().setShowWithinAreaPoint(true);
        chart.getStyler().setLegendPosition(Styler.LegendPosition.OutsideS);
        chart.getStyler().setLegendLayout(Styler.LegendLayout.Horizontal);

        chart.getStyler().setPlotContentSize(1);
        chart.getStyler().setShowValue(true);

        int[] xData = new int[5];
        xData[0] = 10;
        xData[1] = 20;
        xData[2] = 30;
        xData[3] = 40;
        xData[4] = 50;

        int[] yData = new int[5];
        yData[0] = 10;
        yData[1] = 20;
        yData[2] = 30;
        yData[3] = 40;
        yData[4] = 50;

        int[][] heatData = new int[5][5];
        for (int i = 0; i < 5; ++i) {
            for (int j = 0; j < 5; ++j) {
                heatData[i][j] = i + j;
            }
        }

        chart.addSeries("Basic HeatMap", xData, yData, heatData);
    
        SwingNode swingNode = new SwingNode();
        createAndSetSwingContent(swingNode, chart);
    
        vBox.getChildren().add(swingNode);
        VBox.setVgrow(swingNode, Priority.ALWAYS);
    
        Scene scene = new Scene(vBox, 500, 350);

        stage.setTitle("Demo");
        stage.setScene(scene);
        stage.show();
    }

    private void createAndSetSwingContent(SwingNode swingNode, HeatMapChart chart) {
            SwingUtilities.invokeLater(new Runnable() {
                @Override
                public void run() {
                   JPanel chartPanel = new XChartPanel<>(chart);
                   swingNode.setContent(chartPanel);
                }
            });
    }

    public static void main(String[] args) {
        launch(args);
    }
}

On running the application I usually get the following black window:

black window

On slightly dragging the window I get the window with the chart plotted:

chart

In some cases, on launching the app the plot is displayed at once without the black screen. In this case when I resize/move the window the black screen may appear again and I have to drag the window again and so on.

The problem does not appear when the app runs on a Virtual Machine (Windows 10) with total video memory 4 MB without enabling 3D support feature. In this case the app runs correctly with no displaying problems.

Is there a way to avoid this behavior?

0

There are 0 answers