File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Swing / AWT / SWT and the fly likes Anyone familiar with JFreeChart? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Java Interview Guide this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Anyone familiar with JFreeChart?" Watch "Anyone familiar with JFreeChart?" New topic

Anyone familiar with JFreeChart?

Michael Keller

Joined: Jul 06, 2009
Posts: 10
I seem to be having trouble getting my charts to update when I want them to do so. I know that when you add a dataseries to a chart it registers with that chart so that whenever the data is updated, the chart gets updated. But I have a weird problem with that. I've created my chart and my dataseries, and I have a loop like this:

But the chart doesn't update until it runs through the loop entirely. I have to wait until the loop is completed to see anything. I thought maybe my computer, being slow, was just not getting around to showing the chart until the loop was done anyway, so I tried making that '100' into '1000'. All that did was make it take 10x as long to show the chart.

So I thought I could trick it, by forcing it to update. I made my loop do this:

I thought that by creating a new chart and showing a new image I could force it to update, even if it was very slow. But it showed the exact same behavior, the chart wouldn't update until my loop was finished. I even tried creating an entirely new dataset each time through a loop, creating a new series, adding all my data to the series, adding the series to the dataset, plotting a new chart with that dataset, creating a new image from the chart, and then updating the jlabel with that image. This too showed the same behavior. Maybe I'm just a Java greenhorn, but it doesn't make much sense to me.

Thanks everyone for your friendly help,
Ernest Friedman-Hill
author and iconoclast

Joined: Jul 08, 2003
Posts: 24199

Hi Michael,

I'm wondering if this is a threading issue. Java GUIs get updated from a particular thread, the same one that event handlers run in (often called the EDT, the "event dispatch thread.") If all the code you've shown is running in an event handler, then by definition, the chart can't update until your code is through running, when that thread becomes free.

If you want live updates, then your loop has to run in a different thread; you could try something like

[Jess in Action][AskingGoodQuestions]
Michael Keller

Joined: Jul 06, 2009
Posts: 10

Thank you for the advice. I actually suspected that the issue might be a threading one, but that only seems to me to explain the problem with the first loop. In my second try, I had it creating a new chart and updating the jlabel in the same loop, which should then be in the same thread.
Rob Spoor

Joined: Oct 27, 2005
Posts: 20279

This is most definitely a Swing threading issue.

A different background thread is most definitely what you want, but what Ernest has not mentioned is that all code that modifies the GUI directly should be run in the EDT. You can use EventQueue.invokeLater for that. Or, if you're using Java 6, use SwingWorker with its doInBackground, publish, process and done methods:
- doInBackground runs your code in a different thread
- publish is called in this thread
- process is called on the EDT with one or more objects passed to a call to publish. These objects are cached in between calls, so don't expect one call of process to receive data that has been sent by the last call to publish only. You should check the entire List
- done is called on the EDT when doInBackground finishes

I'm moving this to the Swing forum.

How To Ask Questions How To Answer Questions
Michael Keller

Joined: Jul 06, 2009
Posts: 10
Rob, Ernest,

Thanks for your help! I created a thread for it and that ended up doing what I wanted it to do. I knew I'd have to use threading eventually for this project, but I thought I could test it without them first. It does make sense now.

- Mike
It is sorta covered in the JavaRanch Style Guide.
subject: Anyone familiar with JFreeChart?
jQuery in Action, 3rd edition