How to display animation in a JTable cell

前端 未结 2 1411
离开以前
离开以前 2020-12-06 06:52

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

相关标签:
2条回答
  • 2020-12-06 07:15

    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

    0 讨论(0)
  • 2020-12-06 07:34

    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.

    0 讨论(0)
提交回复
热议问题