Search code examples
javamultithreadingswinggraphjung

Should updates to a JUNG graph be done on the EDT?


I have a JUNG graph that is constantly be updated (new vertices, removing vertices, and updating existing vertices). All of this work is done in a bunch of custom classes that run on their own thread waiting for updates from an external source, then updating appropriately.

I now want to visualize the graph, so I retrieve a reference to the graph and set it in a Layout which is given to a VisualizationViewer. When updates come in, they are processed in the other thread, then I call VisualizationViewer.repaint() to refresh the graph.

My question is, should I be doing all the work updating the graph object on the EDT? Or is it alright to do the work in a separate thread, then just call vv.repaint() like I'm doing now? Not sure if helpful/related, but while most of the updates are coming from an external source, the user can still manually delete things in the graph through the GUI.

Thanks


Solution

  • Jonathan's answer is incorrect, you do not need to update the graph object on the EDT.

    Your VisualizationViewer should always be on the EDT, yes, but using functions like addVertex or addEdge can be called from any thread. The important thing to remember is that you cannot call vv.repaint() from the same thread that you are updating on.

    I achieve this by adding a PropertyChangeListener on the JPanel holding the VisualizationViewer. This listens to changes in the graph and calls repaint accordingly.