views:

1064

answers:

2

I am looking for a way to display an animated progress indicator (animated GIF, a rotating wheel rendered through Java2D etc., no preference here) in a table cell until the value that is to be finally displayed has been computed or retrieved.

So far I have just put a static "pending..." text into each cell that is not yet ready to speed up display. As data arrives from a background thread I put it into the appropriate cell and call repaint on the table.

With a static label or image this works fine, but because the cell rendering uses this only as sort of a rubber stamp and does not create separate instances per cell (which of course is a good idea performance-wise) an animated GIF for example does not "play" in the individual cells.

How can I achieve such an effect? It would make for a more "polished" UI than just a simple text.

+2  A: 

As you say, the basic JTable rendering mechanism does not support this, a CellRenderer is used to paint a cell (or part of a cell as necessary), and won't allow repaints, it draws on demand.

Method 1 - JLayeredPane

If I needed to do this I would use a JLayeredPane to draw my animated renderers on top of the the JTable.

However I think it would be a lot of work. You'd need to get quite a lot right, especially if you had it within a scrollpane. You containment heirarchy would be something like:

JScrollPane -> (Viewport) JLayeredPane -> JTable & JPanel (transparent, with animations)

You'd also have a tricky job laying our your panel for animations, you'd either need a custom layout manager to mirror JTable column widths, or use something like a GridLayout. Do-able, but a lot of work.

Method2 - custom CellRenderer and repaint in Swing Timer

The only alternative, would be to keep a list of cells that were animated, and then maintain a swing timer that called repaint on them repeatedly. You would then have a custom CellRenderer doing the animation painting, with painting changing with time. This might be simpler, but getting the frame rate right without eating CPU, and getting updates of repaints to match animation updates could lead to some odd visual effects

Nick Fortescue
+1  A: 

First I went with the same idea Nick Fortescue had (Method2). However just after that I stumbled across

The rabbit hole blog

where the author provides very usefu classes for animated icons (based on GIFs) as well as a Java2D drawn infinite progress indicator (the famous Mac OS X like spinning wheel) which I could just drop into my application without having to worry about too much painting myself.

However had I not found these, Nick's way would have been it.

Daniel Schneller