2

The issue I'm having is that the mxGraph object does not load correctly on startup. It requires me to click a cell inside of it for it to appear, and even then it does not reveal the entire graph, just the cell that was clicked. In order to reveal all of it I have to refresh the graph via another control or drag a cell around the entire graph area.

When I initially developed this I was working with Java 8 and this was not an issue. This has only occurred since updating to Java 11 (OpenJDK). Everything else was kept the same when upgrading to 11, only the updated dependencies changed.

I am wrapping the mxGraphComponent inside of a SwingNode in order to place it inside of a JavaFX node. I've had issues in the past with Swing nodes inside JavaFX but I am creating all of the Swing components using the SwingUtilities.invokeLater() method. I am using the 3.9.8.1 version of JGraphX from Maven, but I have also tried the updated 4.0.0 from GitHub with no success.

Here's my MCVE:

public final class Main {

    public static void main(final String[] args) {
        Application.launch(JGraphExample.class);
    }
}

public final class JGraphExample extends Application {

    private mxGraph graph;

    @Override
    public void start(final Stage primaryStage) {
        final SwingNode value = new SwingNode();

        final BorderPane root = new BorderPane();
        root.setCenter(value);
        root.setBottom(createRefreshButton());

        SwingUtilities.invokeLater(() -> value.setContent(buildGraphComponent()));

        primaryStage.setWidth(500);
        primaryStage.setHeight(500);
        primaryStage.setScene(new Scene(root));
        primaryStage.show();
    }

    private mxGraphComponent buildGraphComponent() {
        graph = buildGraph();

        return new mxGraphComponent(graph);
    }

    private mxGraph buildGraph() {
        final mxGraph graph = new mxGraph();
        graph.insertVertex(graph.getDefaultParent(), "x", "Hello", 100, 100, 100, 100);

        return graph;
    }

    private Button createRefreshButton() {
        final Button refresh = new Button("Refresh");
        refresh.setOnAction(actionEvent -> graph.refresh());

        return refresh;
    }
}

Until clicking the refresh button the graph will not render correctly. This was not the case with Java 8 as it worked as intended. It seems the update to 11 has teased this issue out.

Has anyone come across this before or have any ideas? Thanks in advance.

Dan Beavis
  • 31
  • 3
  • 1
    If you end up not being able to get JGraphX to work properly, and you don't want to use the built-in JavaFX Chart API for whatever reason (missing features, don't like it, etc.), you might consider [JFreeChart](https://github.com/jfree/jfreechart). – Slaw Apr 09 '19 at 10:05
  • 1
    Are you sure that it is related to `JGraphX ` ? Try testing with `buildGraphComponent()` that returns say a `JPanel`. – c0der Apr 09 '19 at 12:06
  • @Slaw I've used JFreeChart in the past but I need to use a directed graph for my application and AFAIK they do not support that graph type. – Dan Beavis Apr 10 '19 at 10:25
  • @c0der It may not be specific to JGraphX, it could be related to Java 11. Either way it has come about as a combination of the two. I have also tried wrapping the mxGraphComponent inside a JPanel (as per your suggestion), then adding to my SwingNode, and the same issue occurs. – Dan Beavis Apr 10 '19 at 10:32
  • It does matter for a. identifying the problem b. reproducing it. MCVE requires removal of any resource which is not essential. If the problem is reproduced with say `private JPanel buildGraphComponent() { JLabel l = new JLabel("Test"); JPanel p = new JPanel(); p.setPreferredSize(new Dimension(300,200)); p.add(l); return p; }` it makes it easier to identify and help. – c0der Apr 10 '19 at 11:28

0 Answers0