且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何在JTable中单元格中显示动画

更新时间:2023-12-03 13:11:10

正如你所说,基本JTable的渲染机制,不支持这一点,CellRenderer中用于绘制(必要时进行细胞或部分)的单元格,不允许重新绘制,它借鉴了​​要求。

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.

如果我需要做到这一点我会用一个的JLayeredPane 上绘制JTable中的***动画我的渲染器。

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中 - >(视)的JLayeredPane - >的JTable和放大器; JPanel的(透明,动画)

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

您也就会有一个棘手的工作奠定我们您的动画面板,你要么需要一个自定义布局管理器,以反映JTable中列的宽度,或使用像一个网格布局。 DO-能干,但大量的工作。

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.

唯一的选择,将继续进行动画的细胞的列表,然后维持反复对他们所谓的重画秋千计时器。然后,您将有一个自定义CellRenderer做动画的绘画,绘画随时间变化。这可能是简单的,但得到的帧速率权没有吃的CPU,并获得重新绘制,以配合动画更新的更新可能会导致一些奇怪的视觉效果。

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