Meaning of getTableCellRendererComponent argument

余生颓废 提交于 2019-12-02 16:00:53

问题


This is my very first StackOverflow question, so if I do/ask something incredibly stupid, please be lenient!

I'm a noob when it comes to Java, specifically swing, and I'm having trouble understanding the getTableCellRendererComponent function of the TableCellRenderer interface (and similar with the getTableCellEditorComponent function of the TableCellEditor interface). Specifically, I don't understand the point of the 2nd argument (Object type). Doesn't the value for this just come from the given JTable at the given row and column? If so, why bother with the 2nd argument at all? I've done quite a lot of google searching, but no-one appears to answer this (or perhaps my brain has some malfunctioning neurones, which, given past history, isn't an unlikely explanation...)

I would appreciate the help!

Thanks


回答1:


JTable rendering is an example of the flyweight pattern in which a single component is moved repeatedly to render a table cell's value. The getTableCellRendererComponent() is called by the table each time the table determines the need to render a cell. As it may be called frequenly, some care should be given to efficiency. The advantage is that the rendering process can be omitted for non-visible cells. See also this related example.

Addendum: Why not just simply call table.getValueAt(row, column) instead of using value?

When it paints a cell, the table's UI delegate, typically a subclass of BasicTableUI, must invoke prepareRenderer() without knowledge of the TableModel. The contract of the table's prepareRenderer() method, which does have knowledge of the TableModel, specifies that it "Prepares the renderer by querying the data model for the value." See A Swing Architecture Overview for details.



来源:https://stackoverflow.com/questions/18471615/meaning-of-gettablecellrenderercomponent-argument

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!