如何在 JTable 单元格中显示动画 [英] How to display animation in a JTable cell

查看:31
本文介绍了如何在 JTable 单元格中显示动画的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找一种在表格单元格中显示动画进度指示器(动画 GIF、通过 Java2D 渲染的旋转轮等,此处无偏好)的方法,直到计算或检索到最终要显示的值.

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.

到目前为止,我只是在每个尚未准备好加速显示的单元格中放入一个静态的待处理..."文本.当数据从后台线程到达时,我将其放入适当的单元格并在表格上调用 repaint.

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.

对于静态标签或图像,这工作正常,但因为单元格渲染仅将其用作橡皮图章,并且不会为每个单元格创建单独的实例(这当然是性能方面的好主意)动画 GIF例如不会在单个单元格中播放".

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.

推荐答案

如你所说,基本的 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 -> (Viewport) JLayeredPane -> JTable &JPanel(透明,带动画)

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

你也有一个棘手的工作来放置我们的动画面板,你需要一个自定义布局管理器来镜像 JTable 列宽,或者使用类似 GridLayout 的东西.可行,但工作量很大.

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

这篇关于如何在 JTable 单元格中显示动画的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆