Skip to content
Advertisement

How to draw a SimpleWeightedGraph on a JPanel?

I have a SimpleWeightedGraph and I want to draw it on a JPanel in a JFrame. Unfortunately nothing is drawn.

I read this article. They are using a ListenableDirectedGraph so I tried a ListenableUndirectedGraph with no success.

JavaScript

Advertisement

Answer

It looks that you’re leaving some important details out of your question, and without a Minimal, Complete, and Verifiable example it is hard to say where is the problem.

However, note that the sample you’re trying to adopt is very old. JGraph has moved on to JGraphX. Consider the following sample that demonstrates the link of JGraphT and JGraphX using JGraphXAdapter.

JavaScript

enter image description here

Note that MyEdge extends DefaultWeightedEdge to provide custom toString() that displays edge weight. A cleaner solution would be probably to override mxGraph.convertValueToString, examine content of cells and provide custom labels as needed. toString is a shortcut for the demo and also I noticed that DefaultWeightedEdge.getWeight() is protected, so the extension is needed anyway 🙂

User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement