且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

难以理解swing的JTable和JTree的渲染器机制

更新时间:2023-12-04 12:30:40

这是 flyweight模式

当JTable重新绘制自身时,它会启动一个循环并迭代每个必须绘制的单元格。

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.