Meaning of getTableCellRendererComponent argument
Asked Answered
K

1

0

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

Kratz answered 27/8, 2013 at 17:9 Comment(2)
Go here docs.oracle.com/javase/6/docs/api/javax/swing/table/… This may helpAround
I looked at that already, but still doesn't make sense. Why not just simply call table.getValueAt(row, column) instead of using "value"?Kratz
A
2

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.

Acetal answered 27/8, 2013 at 17:14 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.