5

I am using a custom cell renderer that implements TableCellRenderer and displays JTextArea (instead of JLabel) for each row. I am basically overriding getTableCellRendererComponent(...) method with mine which does some additional calculations per row. These calculations have to be done just once per table update. Since getTableCellRendererComponent method is being called with every mouse move, lag occurs. So I thought I should prevent getTableCellRendererComponent from being called to avoid lag.

Considerations:

1) My table has only 1 column and has no header.

2) My data is static and is read from an ArrayList by getValueAt(int row, int column) method in a custom tablemodel class implementing AbstractTableModel.

3) I don't need to watch over mouse motion events.

4) I don't expect much data, so I might want to display whole table at once or cache it completely.

5) Most lag is caused by setting text each time when returning from getTableCellRendererComponent, because some rows are using Right-to-Left chars and RTL text requires extra time to render.

  • 1
    just to emphasize @Robin 's answer: never-ever do _any_ calculation in getXXRendererComponent. The frequency of calls is high and completely out of your control. – kleopatra Aug 28 '12 at 06:47
  • @kleopatra k got it. But how do I control internal operations then? Like setText and setFont methods I use (for my JTextArea cell) inside the getCellRendererComponent method. – Ahmet Noyan Kızıltan Aug 28 '12 at 07:04
  • configuring the _renderer_ is fine .. sounds like I'm misunderstanding what you really mean by "calculation". Time for an SSCEE (or at the very least the code of the renderer :-) – kleopatra Aug 28 '12 at 08:54
  • next part is here: http://stackoverflow.com/questions/12160611/caching-jtable-rows – Ahmet Noyan Kızıltan Aug 28 '12 at 13:51

1 Answers1

7

Do not try to limit the number of getTableCellRendererComponent calls. Instead, make your implementation of the renderer better by caching the results of the calculation.

You can easily add a listener to the tablemodel so that your renderer knows when the model is updated. Only then it should mark that the stored calculation results are invalid, and recalculate them on the next getTableCellRendererComponent call.

Robin
  • 36,233
  • 5
  • 47
  • 99