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

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

问题描述

我要寻找一种方式来显示一个动画进度指示器(GIF动画,通过的Java2D等,没有preference这里呈现的一个旋转的轮子)在表格单元格,直到被最后显示的一直是价值计算或检索。

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.

通过静态标签或图片的工作正常,但由于电池渲染使用此仅作为排序一个橡皮图章,并且不建立每个单元单独的实例(这当然是一个好主意性能明智)动画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.

我如何能实现这样的效果?它将使的不仅仅是一个简单的文本更抛光UI。

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中 - >(视)的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

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

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