0

I am rendering a graph from a GraphML source file using either JUNG or Prefuse. Now another program updates this source file periodically with new nodes and edges. I want to update the graph to reflect these changes as well. I cannot find anything in both APIs themselves that let me do this (no appropriate refresh(TIMER) or redraw(TIMER) kind of methods). The one way of doing this might be to terminate the JFrame after some time period and redo the whole data loading, visualization and rendering process again, but that doesn't seem proper to me.

So the question is, what's the best way to do this as simply and as quickly as possible? Thanks :)

xkcd
  • 414
  • 5
  • 19

2 Answers2

1

Check the source file "periodically". If you hang on to a handle f for the file, you can query f.lastModified() and if the other program has done its dirty work since last you checked, update! Similar solutions apply if the other program were writing only an update file (which would be sensible if you have the power to make that change - less file to read, know exactly what to change in your model, etc).

"Periodically" is a bit of sticky wicket - your case is probably simple enough that "every X (milli)seconds, check" will do. Periodic tasks in Java can be implemented in a variety of ways - see, e.g. this other question.

JUNG has re-render/re-paint on update demos, e.g. this one. The tutorial (pdf) linked in their docs covers dynamic updates in section 5 (the section is about interactivity, but the necessaries to do updating when presented with new info are all covered).

Community
  • 1
  • 1
Carl
  • 7,538
  • 1
  • 40
  • 64
  • Thanks @Carl, I think I will follow your approach about re-rendering, although for the file modification I have tried the Apache commons-io with the `FileAlterationObserver` and `FileAlterationListener` classes and it gives a much better logic and operational options. Thanks a lot. – xkcd Apr 30 '12 at 12:18
  • @xkcd those are indeed good alternatives to DIY; best of luck. – Carl Apr 30 '12 at 15:04
0

JUNG provides wrappers (ObservableGraph) that allow you to listen for changes.

Once you've been notified of an update, you'll have to update the layout and then refresh the visualization. The latter is done in the usual way for Swing.

Joshua O'Madadhain
  • 2,704
  • 1
  • 14
  • 18
  • I'm pretty sure that other program isn't going to be modifying the graph via the `ObservableGraph` instance, so no modification events would be fired. – Carl Apr 26 '12 at 16:12
  • Exactly right. The other program is not even aware of the existence of the visualization program. It just feeds data in to the GraphML file. – xkcd Apr 27 '12 at 16:08
  • Ah, I missed that you were talking about updates to the file, not to the Graph. – Joshua O'Madadhain May 04 '12 at 21:43