JTable + TableModel缓存获取事件的延迟实例化? [英] JTable + TableModel cache fetch event for lazy instantiation?

查看:83
本文介绍了JTable + TableModel缓存获取事件的延迟实例化?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

场景:

您正在使用带有自定义TableModel的JTable来查看位于数据库或网络中或任何其他位置的某些集合的内容.

you are using a JTable with a custom TableModel to view the contents of some collection located in a database or on the network or whatever.

完成这项工作的蛮力方式是立即加载整个集合.可以说,由于需要的资源,这是不切实际的.

The brute force way to make this work is to load the whole collection at once. Let's say that isn't practical because of the resources needed.

解决该问题的简单方法是,在JTable呈现每一行并调用TableModel.getValueAt()时,按需获取行,一次获取一行.必要时进行缓存.不过,这会对数据库造成很大的冲击.

The simple way around that problem is to fetch rows on demand, one row at a time, as the JTable renders each row, and calls TableModel.getValueAt(); cache as necessary. This causes a lot of hits to the database, though.

有没有一种方法可以监听JTable的滚动/视口事件,以弄清楚在呈现每个单元格之前它将显示哪些行?如果是这样,我想截取并导致我的自定义TableModel一次预取一页.

Is there a way to listen to scroll/viewport events for a JTable, to figure out what rows it is going to display before it renders each cell? If so, I would like to intercept and cause my custom TableModel to prefetch one page at a time.

编辑:为了说明,此处的要点是能够批量获取一组可见表行的内容,而不必自己获取每行的内容

edit: just to clarify, the point here is to be able to fetch the contents of a group of visible table rows in one batch, rather than having to fetch the contents of each row by itself.

推荐答案

实际上,这正是JTable允许的.据我所知 如果getRowCount()方法恰好反映了有多少条记录, 然后在绘制单元格时,仅查询可见部分. 我认为通过侦听视口进行预取不会更快.

In effect, that is exactly what JTable allows. afaik if the getRowCount() method reflects exactly how many records there are, then when the cells are painted, only the visible part is queried. I don't think prefetching by listening on the viewport would be any faster.

您可以等待所有getvalue请求.记录这些,返回"null"或已经缓存的值.然后在未说20毫秒的getvalue调用之后,对所有记录的单元格进行实际请求.并在模型上触发rowUpdated事件,以便JTable重新绘制.

you could wait for all getvalue requests. record those, return "null" or the already cached value. then after a getvalue isn't called for say 20 ms do the actual request for all the recorded cells. and fire rowUpdated events on the model so JTable repaints again.

** **您可以维护在模型上查询的最后一条记录的列表.您的列表不必超过屏幕上可见的行数.在几秒钟不查询getValue()之后,您可以向后端执行此异步批量请求

**[edit]**You could maintain a list of the last records queried on the model. your list doesn't need to be any longer than the amount of rows visible on the screen. after getValue() isn't queried for a few ms, you could perform this async bulk request to the back-end

这里唯一要注意的是排序/过滤算法. 当您查询视口并让数据依赖于该视口时, 数据与视图之间的1-1关系. JTable本身没有的东西. 但是我想那是没有办法的. 我将使IDE调试器能够挖掘sun代码.然后查看其渲染实现如何找出要重画的行.我不知道.

The only catch here is the sorting/filtering algorithm. When you query the viewport and let the data depend on that, then there is a 1-1 relation between your data and the view. Something which JTable itself doesn't have. But i guess there is no way around that. I would enable the IDE debugger to dig through sun code. and then see how their rendering implementation finds out which rows to repaint. i don't know by heart.

这篇关于JTable + TableModel缓存获取事件的延迟实例化?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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