减少getTableCellRendererComponent调用的次数 [英] Reduce number of getTableCellRendererComponent calls

查看:124
本文介绍了减少getTableCellRendererComponent调用的次数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用实现TableCellRenderer的自定义单元格渲染器,并为每行显示JTextArea(而不是JLabel).我基本上是用我的方法重写getTableCellRendererComponent(...)方法,该方法对每行进行一些额外的计算.每个表更新只需要执行一次这些计算.由于每次鼠标移动都会调用getTableCellRendererComponent方法,因此会出现延迟.所以我认为我应该防止调用getTableCellRendererComponent以避免出现滞后.

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.

注意事项:

1)我的表只有1列,没有标题.

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

2)我的数据是静态的,并通过实现AbstractTableModel的自定义表模型类中的getValueAt(int row,int column)方法从ArrayList中读取.

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)我不需要监视鼠标移动事件.

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

4)我不希望有太多数据,所以我可能想一次显示整个表或将其完全缓存.

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

5)大多数滞后是由于每次从getTableCellRendererComponent返回时都设置文本引起的,因为某些行使用的是从右到左字符,而RTL文本需要额外的时间来渲染.

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.

推荐答案

请勿尝试限制getTableCellRendererComponent调用的次数.而是通过缓存计算结果来改善渲染器的实现.

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.

您可以轻松地向表模型添加侦听器,以便渲染器知道何时更新模型.只有这样,它才应标记存储的计算结果无效,并在下一个getTableCellRendererComponent调用中重新计算它们.

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.

这篇关于减少getTableCellRendererComponent调用的次数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆