在 JScrollPane 中查找可见组件的快速方法 [英] Fast way to find visible components in JScrollPane
问题描述
给定一个包含一千个组件的 JScrollPane
使用一些 LayoutManager
.每个组件都可以处于加载或卸载状态.没有足够的资源供所有加载.
Given a JScrollPane
containing a thousand components using some LayoutManager
. Each component can be either in a loaded or unloaded state. There are not enough resources for all to be loaded.
我只想让内存中的可见组件 load()
ed,而不可见组件 unload()
ed.当用户滚动时,侦听器需要不断更新组件的状态:unload()
以前可见,load()
新可见.
I'd like to have only the visible components load()
ed in memory, and the invisible components unload()
ed. When the user scrolls, a listener needs to keep updating the components' states: unload()
the previously visible, and load()
the newly visible.
- 在任何给定时刻,我如何知道哪些组件是可见的?
- 我可以在不迭代整个一千个的情况下知道这一点吗?(就像一个高效的
viewPort.getVisibleComponents()
)
我将准备好所有组件 Y 的排序列表,然后在运行时二进制搜索 ViewPort
的 Y 以达到一个索引,该索引可以引导我找到可见的.这失败了,因为组件 Ys 在列表准备期间都返回 0.这需要高效.
I was going to have a prepared sorted list of all component Ys, then in runtime binary search the ViewPort
's Y to reach an index which may guide me to the visible ones. This failed as component Ys all returned 0 during list preparation time. This needs to be efficient.
推荐答案
为了具体起见,我将假设有 1000 条记录,每条记录由 class Record
的一个实例表示.每个这样的 Record
应该包含一个唯一的、有意义的 key
,类型为 Key
.
For concreteness, I will assume 1000's of records, each represented by an instance of class Record
. Each such Record
should contain a unique, meaningful key
of type Key
.
获取
List
,可能在后台任务中.
构建一列key
值的 TableModel.
Construct a one-column TableModel
of the key
values.
使用这个模型构造一个JTable
、table
.
Use this model to construct a JTable
, table
.
将 table
添加到合适布局左侧的 JScrollPane
.
Add the table
to a JScrollPane
on the left side of a suitable layout.
在右侧,添加一个面板,该面板可以显示
单个选定记录
的详细信息.
On the right, add a panel that can Display
the details of a single, selected Record
.
让 Display
包含一个 ListSelectionListener
,见这里a>,更新当前在 table
中选择的任何 Record
的详细信息.
Let Display
contain a ListSelectionListener
, seen here, that updates the details for whichever Record
is currently selected in the table
.
将每个选定的Record
添加到缓存,检索从存储中记录
,可能在后台任务中,仅当尚未存在于缓存中时.
Add each selected Record
to a cache, retrieving the Record
from storage, possibly in a background task, only if not already present in the cache.
这篇关于在 JScrollPane 中查找可见组件的快速方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!