ListState
A hoistable state holder for the selection of a ListBox, carrying the gesture that brings one of its rows into view.
selectedIndices is two-way: assigning it selects those rows, and the user selecting other ones - by click, drag or keyboard - writes them back here. It is snapshot-observable, so reading it inside a composable (or a snapshotFlow collector) subscribes to the user's later selecting as well.
The rows this state names are the composition's own: they are re-applied on every pass, so a list driven by a state never stands on a selection the state does not hold, and a row the items do not reach is left out of the list while it goes on being named here - items that reach it again show it selected.
revealIndex brings one row into view when the application decides to - a row just added, a search hit:
val state = rememberListState()
Button("Add", onClick = { items = items + Item() })
LaunchedEffect(items) { state.revealIndex(items.lastIndex) }
ScrollPane {
ListBox(items = items, state = state, modifier = SwingModifier.viewport())
}itemCount and shownSelectedIndices answer for the list instead of for what this state holds. Each reads the bound list where it is called, so what it reports is what the list stands on - which is not always what was declared, since a selection mode narrower than the declaration keeps only part of it and an index the items do not reach has no row to be selected at. They are not snapshot state, so reading one subscribes to nothing; a composable that has to follow the user reads selectedIndices. An unbound state has no list to answer for and reports no items and nothing selected.
A state drives at most one list: passing it to a second one moves it there and leaves the first unbound.
Parameters
the rows selected until the caller or the user moves the selection.
See also
Constructors
Properties
The selected row indices, expressed as the general multi-select shape so one state covers every one of org.jetbrains.compose.swing.constants.SelectionMode's modes.
The rows the list has selected, as indices into its items. Empty while no list is bound.
Functions
Brings the row at index into view and returns whether it was reached.