JavaFX - Switching Scenes Like A Boss! (Part 1)

preview_player
Показать описание
Covers how to switch between JavaFX Scenes on the same Stage.

The design incorporates the use of a Stage Manager which, with the help of the Spring Framework will allow you to dependency inject it anywhere into your code base and request a Scene swap.

The example demonstrated how to switch from the Login Scene to the Main application Scene however the architecture allows you to scale to as many Scenes as you want without having to copy paste your code everywhere like is shown in other tutorials.

Simply put, after this tutorial you'll be switching Scenes like a boss!

Code is available on GitHub:

Ask me A Question ...

Subscribe to MVP Java ...

Follow me ...
Рекомендации по теме
Комментарии
Автор

Thank you! This is the most heplful JavaFX tutorial that i've ever found in YouTube.

AndrewKobrin
Автор

About 6 years ago I had to do a project in JavaFX. We started it but it was soon abandoned but I kept on wondering how to do this and you absolutely nailed it. Thanks so much for this video, good job (yeah, yeah it's 2020 now...)

Gerhard
Автор

Holy shit you are a programming god, this was very helpful, and your code is very clean

cyborgplay
Автор

I am looking for a way to save the recent changes made in my application until closing and return to this state after reopening the application. need help please !

ducourrier
Автор

great tutorial!!!! another way I like to do this is by using just one scene and one container layout that'll hold the rest of my layouts by using the FXML Loader class. this way if I have navigation buttons to the left for instance I don't have to keep placing them in every scene. I'll just set the buttons to replace the layouts.

semicolonkeese
Автор

thanks a lot man for this tuto
i have one question if you allow me :
im working in a project which i have to switch scenes but keep the same " left navigation bar " and the same " header " for all scenes and i got stuck on this point
i worked around by this :
keeping the same scene and changing only the components of a central anchor pane by using the method
this solution seems to be unprofessional and slow
so can you post a solution for this kind of situations, and thanks a lot in advance

hakoamin
Автор

Thanks for this great turorial + code!!

georgfunk
Автор

This is pretty clean and easily maintainable code.
I have a doubt in switching between scenes. What if I want to configure stage properties based on the screen, something like I want to fill some screen with different color, or I want to implement a stage's setOnShow event?

akhilanilkumar-dev
Автор

The StageManager is an awesome abstraction. But it is a singleton that contains state (the Stage object). In most online documentation I have read that singletons are safer if they do not contain any state. What are your thoughts on this? I understand that it is immutable state, since it can't be modified after the object is created (no setter methods). But what if I wanted to make it mutable? Would you suggest against it?

chronpwn
Автор

Thank you a lot about this.
I try to use some of your idea in my 1st JavaFX + Spring + Maven desktop app, but i get a "No qualifying bean of type 'myChildrenPackage. StageManager' available". Have U any clue for a solution?
Thank's again!
(Java11 + openJFx)

fruityhedgehog
Автор

hi thank for your video, can you comment the link of video "how to setup javafx and spring" below pleas

mohammedjunaid
Автор

This is really great. I will use it in the future.

mynameislwan
Автор

Great tutorial.I would like to know how we can create multiple stages.For example I would like to pop up multiple stages when clicked on various buttons placed in the main stage.In that case will we create new stage as Stage s = new Stage()?If so will it be mananged by Spring?Or is there any other way to create it?

vikasv
Автор

Very interesting tuto, but i have a probleme with the connected user credentials, i can't get them after the connection from the controller : when i use this code on the controller of the view :
Object principal =
String username="tt";
if (principal instanceof UserDetails) {
username =
} else {
username = principal.toString();
}
return username;
i got the Null point exception error

LightUpMindset
Автор

hi :) can you help me?? how to use button.performclick() like c# in javaFx???

by_on
Автор

Thank you for this tutorial, i came a cross a litte issue with Java 9 . This ideas works very well with Java 8, so how to make this works with Java 9.
Thank you !

abdobaamaer
Автор

Hi Andy great tutorial!

I'm kind of stuck, I have the basic classes with a plain main window (blank window) but when I try to run the code I get
Caused by: Error creating bean with name 'springFXMLLoader': Requested bean is currently in creation: Is there an unresolvable circular reference?

I've checked and the code is just as you have it, so as far as I understand:
When creating the spring context Springs check the AppJavaConfig class and in the StageManager doesn't throw the error on Stage because of the Lazy annotation but it has an @Autowire for springFXMLLoader that also requires the stage

The exact line that throws the error is:

@Override
public void init() throws Exception{
springContext =
}

As you can see I even got rid of the bootstrap method just in case. The SpringFXMLLoader, AppJavaConfig and StageManager are copied from the code you provided.

If you could throw some light into my problem it would be great, because a centralized stage manager like this is exactly what I was looking for.

thx

metathlon
Автор

Hello, thank of lot for the good video and the source code ^^.
I noticed that if you change the version of spring like spring >= 2.0.0 is not ok...
in pom.xml: <parent>



<relativePath/> <!-- lookup parent from repository -->
</parent>

I have this error: Caused by: No qualifying bean of type available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {}

But if i return to , it's ok....
Can you explain the difference please ?
Thank you

surfeurdestemps
Автор

hi. im from ukraine
great tutorials but hard to understand.
i making desktop car service management project for company where i am working now
you speak so fast and code is already wrote(
by the way i followed you on facebook, thanks for videos

romanucountry
Автор

Before you spoke I was 99.9% sure you would talk with an Indian accent. Thank you for not doing that. I have nothing against people from India but programming can be hard enough without being able to fully understand the words that are being said.

RedEyedJedi