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

queenbee
  • 155
  • 1
  • 7
  • Go here http://docs.oracle.com/javase/6/docs/api/javax/swing/table/TableCellRenderer.html This may help – Yubaraj Aug 27 '13 at 17:17
  • I looked at that already, but still doesn't make sense. Why not just simply call table.getValueAt(row, column) instead of using "value"? – queenbee Aug 27 '13 at 17:26

1 Answers1

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.

Community
  • 1
  • 1
trashgod
  • 196,350
  • 25
  • 213
  • 918