为什么单元格渲染器经常扩展JLabel? [英] Why do cell renderers often extend JLabel?

查看:156
本文介绍了为什么单元格渲染器经常扩展JLabel?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我注意到这很常见。例如,DefaultListCellRenderer,DefaultTableCellRenderer和DefaultTreeCellRenderer都使用它。我在网上看到的很多自定义单元格渲染器也使用它。我想在我的代码中使用自定义TableCellRenderer,但我对是否真的需要继承JLabel感到困惑。子类化JLabel有什么好处?

I noticed this is common. For example DefaultListCellRenderer, DefaultTableCellRenderer, and DefaultTreeCellRenderer all use it. Also a lot of the custom cell renderers I see online use this as well. I want to use a custom TableCellRenderer in my code, but I'm confused about whether I really need to subclass JLabel. What's the benefit of subclassing JLabel?

推荐答案

DefaultTableCellRenderer的API


表类定义单个单元格渲染器,并将其用作橡皮图章,用于渲染表格中的所有单元格;它渲染第一个单元格,更改该单元格渲染器的内容,将原点移动到新位置,重新绘制它,依此类推。标准的 JLabel 组件并非设计为以这种方式使用,我们希望每次都能避免触发 revalidate 绘制。这会大大降低性能,因为 revalidate 消息将在容器的层次结构中向上传递,以确定是否会影响任何其他组件。由于渲染器仅在绘制操作的生命周期中具有父级,因此我们同样希望避免与绘制操作的层次结构相关联的开销。所以这个类会覆盖 validate invalidate revalidate 重绘 firePropertyChange 方法为无操作并覆盖 isOpaque 仅用于提高性能的方法。如果您编写自己的渲染器,请记住这一性能考虑因素。

The table class defines a single cell renderer and uses it as a as a rubber-stamp for rendering all cells in the table; it renders the first cell, changes the contents of that cell renderer, shifts the origin to the new location, re-draws it, and so on. The standard JLabel component was not designed to be used this way and we want to avoid triggering a revalidate each time the cell is drawn. This would greatly decrease performance because the revalidate message would be passed up the hierarchy of the container to determine whether any other components would be affected. As the renderer is only parented for the lifetime of a painting operation we similarly want to avoid the overhead associated with walking the hierarchy for painting operations. So this class overrides the validate, invalidate, revalidate, repaint, and firePropertyChange methods to be no-ops and override the isOpaque method solely to improve performance. If you write your own renderer, please keep this performance consideration in mind.

这篇关于为什么单元格渲染器经常扩展JLabel?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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