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.
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.
- At any given moment, how do I know which components are visible?
- Can I know this without iterating the whole thousand? (as if an efficient
viewPort.getVisibleComponents()
)
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.
For concreteness, I will assume 1000's of records, each represented by an instance of
class Record
. Each suchRecord
should contain a unique, meaningfulkey
of typeKey
.Obtain the
List<Key>
, possibly in a background task.Construct a one-column
TableModel
of thekey
values.Use this model to construct a
JTable
,table
.Add the
table
to aJScrollPane
on the left side of a suitable layout.On the right, add a panel that can
Display
the details of a single, selectedRecord
.Let
Display
contain aListSelectionListener
, seen here, that updates the details for whicheverRecord
is currently selected in thetable
.Add each selected
Record
to a cache, retrieving theRecord
from storage, possibly in a background task, only if not already present in the cache.