Question

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

Was it helpful?

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.

OTHER TIPS

Most GUI updates must be done on the EDT. It would be better to do the changes on it directly and avoid calling repaint unless it is absolutely necessary.

To be sure the code to update the GUI is called on the EDT, you can do that kinda of code :

final JLabel label = yourLabel;
Runnable code = new Runnable() {

  @Override
  public void run() {
    label.setText("SomeText");    
  }
};

if (SwingUtilities.isEventDispatchThread()) {
  code.run();
} else {
  SwingUtilities.invokeLater(code);
}

That way, you don't have to worry from which thread the code is called from and it will update the label.

Edit : To clarify, what you could do is, when you want to update your graph, is to make sure the call to your VisualizationViewer repaint method is done like this

final VisualizationViewer viewer = w;
Runnable code = new Runnable() {

  @Override
  public void run() {
    viewer.repaint();    
  }
};

if (SwingUtilities.isEventDispatchThread()) {
  code.run();
} else {
  SwingUtilities.invokeLater(code);
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top