Java › Swing

Redesigning the Swing restaurant app I built for class

I moved a university Swing app to one window with CardLayout and kept the search results and scroll position when returning from restaurant details.

Redesigning the Swing restaurant app I built for class

I built a restaurant search app for a Java course, and opening a restaurant used to close the results window. I moved both screens into one Swing window so going back takes you to the same restaurant and scroll position.

The application I built for class

I built the app for an object-oriented programming course in my second year of university. I was learning Spring Boot, so I made a separate HTTP backend for the Swing client. For the assignment, I focused on getting the search button to send a request and display the restaurants in the response. Original Swing client · Original Spring Boot backend

When I first wrote about the project in 2024, I found the same request and response code in several places. Some views stored their data in static fields, fonts were loaded in multiple classes, and diagnostics went straight to the console. I wrote up two of the fixes separately when I consolidated logging and moved HTTP calls to Retrofit.

My first plan included a move to Kotlin. I wrote the new screens in refactor-v2 in Java 17 and moved basic browsing to a local H2 catalog. You can open the restaurant list without starting the course project’s server. The interaction I wanted to improve was straightforward. Find a restaurant, open its details, then return to the same spot in the list. RSIS revision used in this post

The old RSIS home screen with a search field and cuisine shortcuts Figure 1. The legacy search home from the project README.

Selecting a restaurant used to close the list window

The original StoreList extends JFrame. Its constructor turns off layout management and positions each panel with setBounds(). When you select a restaurant, the listener calls dispose() on the list frame and opens a detail window for that restaurant ID. Original selection listener

1
2
3
4
5
6
int index = list.getSelectedIndex();
if (index != -1) {
    StoreHeaderDto store = (StoreHeaderDto) listvalues.getElementAt(index);
    dispose();
    new StoreDetail(store.getId());
}

The detail window needs the restaurant ID. Going back to the same spot takes the query, district, selected row, and scroll position. When the list frame goes away, so do the Swing components that held the selection and scroll position. Rebuilding the window means saving those values somewhere and restoring them after the new list has been laid out.

Each screen also sets its own window size and decides where to go when you click a button. The absolute coordinates were quick to lay out for a fixed-size window. A shared sidebar and a resizable main area would have meant repeating that layout and navigation code in every frame.

The sidebar stays while the main panel changes

In the new MainFrame, the sidebar stays on the left. The router uses CardLayout to switch the panel in the center. It builds each screen the first time you open it, then reuses the same panel on later visits. The Swing UI template post shows how I register panels and put the window together. Router implementation

The search panel keeps its list and scroll pane while it’s hidden. When you go back, onEnter() subscribes to the search state again. The list leaves its model alone if the rows still match, so the viewport stays where you left it. When you open a detail view, onLeave() drops the subscriptions and cancels any pending search. If that interrupted a refresh, the screen starts it again on return. Explore screen lifecycle

TaskLegacy pathRefactored path
Open restaurant detailsClose the list window and create a detail windowShow the detail panel in the same window
Return to searchRebuild the list window and restore its stateReuse the existing search panel and list model
Resize the windowThe list window has a fixed sizeThe layout manager resizes the sidebar and center panel

In this app, restaurant details are part of the search, so I keep them in the same window. I’d open separate windows in a tool where you need to compare two documents side by side. For a short confirmation, such as deleting an entry, I’d use a JDialog owned by the window where you started the action.

The refactored RSIS explorer with a sidebar, results, and a selected restaurant Figure 2. Explore shows the search results and the selected restaurant to the right of the shared sidebar.

The RSIS detail preview with menus and recent reviews beside the same sidebar Figure 3. Menus and recent reviews occupy the same area as the search results. Each screenshot came from a separate gallery run.

Verify a round trip from search to details

From the RSIS repository, run the router and search controller tests with Java 17. Then start the app to check what happens to the selected row and scrollbar when you go back.

1
2
./gradlew test --tests '*CardLayoutRouterTest' --tests '*ExploreControllerTest'
./gradlew run
  1. Choose a district and scroll past the first few rows. Note the selected restaurant and the position of the scrollbar.
  2. Open the details, then go back. Check that the district, selected row, and scrollbar are where you left them.
  3. Change the search criteria. Check that the pager returns to page one and the list shows the new results.

The same rows could appear onscreen even if the app queried for them again. The controller test records each call to a fake search service and checks that going back to an already loaded list doesn’t add a call. Explore controller tests

What I check when adding a screen

  • Decide whether the new screen belongs in the center panel or needs its own window.
  • Decide which screen should keep the search criteria and selection for Back.
  • Release subscriptions and cancel pending queries when the user leaves. Check that returning restarts only the work that was interrupted.
This post is licensed under CC BY 4.0 by the author.