了解swing JTable和JTree的呈现器机制的困难 [英] Difficulties understanding the renderers mechanism of swing's JTable and JTree

查看:63
本文介绍了了解swing JTable和JTree的呈现器机制的困难的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

通常在使用 JTable JTree 时,用户定义其自己的单元格渲染器.

DefaultTableCellRenderer 继承用户的组件,并实现渲染器方法 getTableCellRendererComponent 是非常常见的.事实证明, DefaultTableCellRenderer 实际上是从JLabel继承的,因此在调用super时(在render方法处)返回自身(此),因此用户的渲染器也可以类似地返回自身(此).>

一切正常.

我的问题是怎么回事?

每次由表调用此方法时,都会为其指定不同的参数,并且输出标签将根据这些参数进行更改.如果确实是该标签的相同实例–难道不应该根据对该方法的最后一次调用对其进行更改吗?

难道这并不意味着该表的所有单元格都受到相同标签实例的感染,该标签实例具有相同的值(上次调用renderer方法的值)?

我已经搜索了整个网络,并在Swing的代码中进行了挖掘,但找不到任何实际上复制输出标签的 clone copy构造函数.我找不到任何证据表明(也许)swing使用反射来每次从头开始重新实例化渲染器.

我已经阅读了JTables上Swing的教程,然后我可以找到下几行:

您可能希望表中的每个单元格都是一个组件.但是,出于性能原因,Swing表的实现方式有所不同.取而代之的是,通常使用单个单元格渲染器来绘制包含相同类型数据的所有单元格.您可以将渲染器视为可配置的墨水戳,该表用于将适当格式的数据标记到每个单元格上.当用户开始编辑单元格的数据时,单元格编辑器将接管该单元格,从而控制该单元格的编辑行为.

他们暗示我说的确实是正确的,但没有解释其实际上是如何实现的.

我听不懂.你们可以吗?

解决方案

它是 flyweight模式的实现.

当JTable重新绘制自身时,它将启动循环并遍历必须绘制的每个单元格.

对于每个单元格,它将使用与该单元格相对应的参数调用渲染器.渲染器返回一个组件.该组件绘制在与当前表格单元格对应的矩形中.

然后调用下一个单元格的渲染器,并将返回的组件(例如,具有不同的文本和颜色)绘制在与该单元格相对应的矩形中,等等.

想象一下,每次调用渲染器时,都会获取返回组件的屏幕快照并将其粘贴到表格单元格中.

Often when using JTable or JTree, user define its own cell renderer.

It is very common to inherit user's component from DefaultTableCellRenderer, and implements the renderer method getTableCellRendererComponent. It turns out that DefaultTableCellRenderer in fact inherits from JLabel, thus returns itself (this) when called to super (at the render method) and thus user's renderer can similarly returns itself (this) as well.

And it all works well.

My question is how can it be?

Each time this method is called by the table, it is given different parameters, and the output label is changed as function of these parameters. If it is indeed the same instance of the label – shouldn't it be changed according to the last call to this method? Wouldn't it mean that all of the table's cells are infect composed of the same label instance, which holds the same value (value of last call to the renderer method)?

I have searched the web, and dig within Swing's code, and could not find any act of clone or copy constructor that actually duplicates the output label. I could not find any evidence that (perhaps) swing uses reflection in order to re-instantiate the renderer each time from scratch.

I have read the Swing's tutorial on JTables, and there I could find the next lines:

You might expect each cell in a table to be a component. However, for performance reasons, Swing tables are implemented differently. Instead, a single cell renderer is generally used to draw all of the cells that contain the same type of data. You can think of the renderer as a configurable ink stamp that the table uses to stamp appropriately formatted data onto each cell. When the user starts to edit a cell's data, a cell editor takes over the cell, controlling the cell's editing behavior.

They give a hint, that indeed what I am saying is correct, but do not explain how its actually being accomplished.

I can't get it. Can any of you?

解决方案

It's an implementation of the flyweight pattern.

When the JTable repaints itself, it starts a loop and iterates over every cell that must be painted.

For each cell, it invokes the renderer with the arguments corresponding to the cell. The renderer returns a component. This component is painted in the rectangle corresponding to the current table cell.

Then the renderer is called for the next cell, and the returned component (which has a different text and color, for example), is painted in the rectangle corresponding to the cell, etc.

Imagine that each time the renderer is called, a screenshot of the returned component is taken and pasted into the table cell.

这篇关于了解swing JTable和JTree的呈现器机制的困难的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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