Preserve Swing list state when closing a detail view
Keep your filters, selected row, and scroll position when you return from a Swing detail view by reusing the search panel and its list model.
When I go back to the restaurant list in RSIS, the filters, selected row, and scroll position are still there. I reuse the search panel and keep its list model when the results still match.
This example uses Java 17 and RSIS
refactor-v2.
Return to the part of the list you were reading
Say you’re comparing menus in one district. You narrow the search to a food category, scroll halfway down the list, and open a restaurant. On your way back, you should be able to open the next one without retracing your steps. Keeping the query lets you run the same search again. Preserving the viewport puts the row you were reading back on screen.
I check both the query count and the scroll position. The controller can serve the previous result from memory and the list can still jump to the top if the view rebuilds its model. Those two failures call for different fixes.
The shared window and sidebar already give the search and detail panels the same container. The router keeps the previous screen in that container, so there’s a list and viewport to return to.
Return to the previous card
CardLayout shows one child at a time. The router adds a card when it creates a screen and uses the route class to look up the screen on later visits. navigate() pushes the current route onto the stack before opening the next one. The Back button pops the previous route and shows its card. Picking a top-level item in the sidebar clears the stack and starts navigation from that menu.
The code below comes from CardLayoutRouter.show(). It calls onLeave() on the current screen, then passes the incoming route to onEnter() before showing the next card.
1
2
3
4
5
6
ScreenLifecycle next = screens.computeIfAbsent(route.getClass(), this::create);
if (currentScreen != null) {
currentScreen.onLeave();
}
next.onEnter(route);
cards.show(container, cardKey(route.getClass()));
Detail routes for two restaurant IDs use the same panel. Each time the panel opens, it reads the ID from the incoming route. The search panel stays attached to the card container until the user comes back. Java 17 CardLayout · RSIS router
Store search results and scroll position
ExploreState holds the search criteria, page, latest result, and selected restaurant. The Swing viewport holds the scroll position. I can test how the controller restores a result without opening a window because it has no viewport to lay out.
When the search panel comes back into view, its subscription delivers the current result immediately. Clearing the model and adding the same rows again would fire removal and insertion events. Swing would have to process both changes and recompute the layout. RestaurantList.setItems() leaves the model alone when the rows match in the same order.
1
2
3
4
5
6
7
8
9
10
public void setItems(List<StoreSummary> items) {
List<StoreSummary> current = java.util.Collections.list(model.elements());
if (current.equals(items)) {
return;
}
model.clear();
model.addAll(items);
hoverIndex = -1;
scheduleImageRefresh();
}
I restore the selection by restaurant ID because sorting can put a different restaurant at the same index. The state keeps the selected ID if it still appears in the results. If the ID disappears, the state picks the first row. The list calls ensureIndexIsVisible() only when the selected index changes. This preserves the user’s scroll position if they had scrolled away from the selected row before leaving. RSIS search state · RSIS list model and selection
Suspend screen work when the card is hidden
I keep the panel on exit and close its active subscriptions. The leave hook also stops the input timer, detaches the image listeners, and invalidates the current search request. On return, the panel subscribes again and receives the last result.
After a completed search, the panel displays the result it already has. If you leave during the initial load, the controller submits the same query again because it has no result to show. If you leave during a refresh, the panel displays the previous page while the controller resubmits the interrupted query. When you leave before the debounce timer fires, the view restarts that timer on return. RSIS screen lifecycle hooks · RSIS controller entry and exit
Choose what to retain
| Situation | Approach | Trade-off |
|---|---|---|
| Users often move between a short list and a detail screen | Reuse the panel and model | Release listeners and image subscriptions on exit |
| Users need fresh results on every visit | Save the criteria and selected ID, then refresh | Choose a fallback if the selected ID drops out of the results |
| The app rebuilds large panels or restores them after restart | Store criteria, a selected ID, and a scroll anchor | Resolve the anchor after the list is populated and laid out |
I keep the panel for navigation within a session. If you discard and recreate it, store the ID of a visible row and that row’s offset within the viewport. A raw scroll offset can point to a different row when the results change. You can use the saved ID to find the same row after inserting items above it or changing the font size.
Figure 1. I applied the Jung-gu district filter, scrolled down the results, and selected a restaurant before opening its details.
Figure 2. After I went back, the same restaurant was selected. The list viewport’s vertical offset was still 86 pixels.
Verify both the controller and the view
Run the controller and router tests in the RSIS checkout. The controller tests check that going back to a completed search makes no extra query and that going back to an interrupted refresh starts it again. The router test exercises Back and the screen’s lifecycle hooks.
1
./gradlew test --tests '*ExploreControllerTest' --tests '*CardLayoutRouterTest'
Then pick a filter with enough results to scroll. Before opening details, note the first visible row, the selected restaurant, and the position of the scrollbar thumb. Compare all three as soon as you press Back. Repeat with a different filter to catch a view that resets its model every time it opens. RSIS controller tests
- Back returns to the existing list panel.
- An unchanged result preserves the list model.
- Restore selection by ID and verify scroll position in the open window.
- Leaving the view releases subscriptions and preserves enough state to resume interrupted work.