Difficulties understanding the renderers mechanism of swing's JTable and JTree

后端 未结 3 629
清歌不尽
清歌不尽 2020-12-02 02:03

Often, when using JTable or JTree user writes and assign it is own specific cell renderer.

It is very common to inherit user\'s component

3条回答
  •  眼角桃花
    2020-12-02 02:37

    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.

提交回复
热议问题