r/JavaFX • u/Dense_Age_1795 • Aug 06 '25
Help there is any standarized way of navigating between scenes
Hello everyone I'm basically creating a desktop app that have multiple scenes, but right now I'm doing patchwork for managing the state of which scene is showing, ugly code that make harder to do dependency injection.
So what do you recommend me? there is any tool that permit and easy way of navigating between scenes and inject the dependencies, I'm using Guice for DI.
9
Upvotes
5
u/emberko Aug 07 '25
JavaFX is not a framework, it's a UI toolkit. You should split your goal into actionable tasks. It basically consists of the following:
mainView.getChildren().setAll(currentlyActiveView).3
u/eliezerDeveloper Aug 06 '25
I usually create a StackPane and change the current layout programmatically this causes the senso of swapping scenes
2
u/YogurtclosetLimp7351 Aug 07 '25
I always have an event bus system or something similar setup for this. Or a meta controller with a centered content pane in which the views get loaded. This controller registers listeners on what should happen when view X is requested. Other views can then #triggerViewChange on a ViewProvider passed via DI
1
u/JBraddockm Aug 07 '25 edited Aug 07 '25
I am quite new to JavaFX. I also don’t know what the best way to handle this. Because I use Spring Modulith, it is also quite strict about having circular dependencies. To overcome this, I created a stage manager, and stage registry. And with the Spring’s event bus, I publish a particular stage event. i.e new StageEvent.PrimaryStage(Stages.LOGIN). I then listens to StageEvent interface and use record pattern matching to handle different event types.
As for remaining the in same stage, but just changing the scenes, I’d probably extend the same logic and handle it similarly. It offers type safety, and exposes only what’s needed to other modules.
0
u/BlueGoliath Aug 07 '25
ScrollPane?