How to display animation in a JTable cell

岁酱吖の 提交于 2019-11-27 23:06:18

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

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!