为什么单元格渲染器经常扩展JLabel?

编程入门 行业动态 更新时间:2024-10-27 20:32:09
本文介绍了为什么单元格渲染器经常扩展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?

本文发布于:2023-11-23 02:04:18,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1619695.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:单元格   渲染器   JLabel

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!