JavaFX: Best practice for navigating between UI screens

user4016255 picture user4016255 · Nov 22, 2016 · Viewed 14.2k times · Source

I want to change UI screens from login.fxml to home.fxml.

Should I change the Stage or the Scene? I'm not sure which is the best practice? Also, can I use a lambda expression for the handler in the controller?

Answer

MordechayS picture MordechayS · Nov 22, 2016

First, let's start out with the Stage .vs. Scene issue:

As known, the JavaFX hierarchy is based on: Stage -> Scene -> Nodes (etc).

See here:

enter image description here

Practically speaking, a rule-of-thumb in my opinion is the future:

  • If you plan on going forward to a different place in the flow of your program (login -> profile, for example) - change the Stage.

  • If you are in the same enviroment (login for the first time -> login after multiple wrong tries) - change the Scene.

As for lambdas: Ahhmmm... if your current Java/JavaFX version has the abillity - there is no reason not to use. For more about controller handlers in Java 8, see this great tutorial.