views:

163

answers:

2

I've customized a JPanel that displays a large, complicated diagram. Depending on the size of the data, it can take a few minutes to render in paintComponent(). I'm looking for a strategy to:

  • draw the component without tying up the event dispatch thread.
  • draw something in the JPanel to let the user know the image is being rendered.
  • periodically update another container's label to show the progress

I've researched this a bit, and I'm wondering if the right strategy is to use a SwingWorker to create a background thread and draw to a BufferedImage. Timers would handle the status updates. Class member variables would hold the status.

Am I on the right track?

+2  A: 

You need to look into using a SwingWorker. You should do the rendering of the diagram in a separate thread. The SwingWorker will help accomplish that.

To get started with multi-threading in concurrency, sun has a great tutorial that should prove very helpful.

jjnguy
+1  A: 

Use background image which is updated by special working thread. Then in JPanel's paintComponent() method just paint this image. The strategy is called double-buffering. You have background and foreground image. If separate thread finish the painting of data then set this image as foreground and foreground load as background. Invalidate JPanel and continue painting on back image if necessary.

Rastislav Komara