I've read several things about Scenes vs Panes / StackPanes in JavaFX. I'm just having a hard time determining if there is an "industry standard". Primarily, I'm wanting to go from Login -> Dashboard -> Other "screens". Is there a preferred / standard for this with regards to scenes vs stackpane? I understand if this question gets closed because of "opinion based" but I'm technically not looking for an opinion; I'm looking for the standard by which JavaFX Applications should be architected.
-
There is no such standard. But in general I think it only really makes sense to change a scene if you need to change aspects that the scene maintains (camera, depth buffering, anti-aliasing, etc). If those are all going to be fixed, keep the same scene and change the root of the scene. If you are OK with keeping all the views loaded, you might also consider a solution along [these lines](http://stackoverflow.com/questions/42569204/is-it-possible-to-reload-the-same-fxml-controller-instance/42586777#42586777) (basically you are implementing a tab pane without the tabs). – James_D Mar 14 '17 at 12:28
2 Answers
AFAIK there's no standard coming from JavaFX core nor any set of guidelines for that matter.
Back in the Swing days we used to rely on CardPane
to attain the desired behavior. Sadly JavaFX core does not provide such container/component out of the box. One could write up a similar container based on StackPane
(the trick is to make sure children nodes in the back layers are hidden from view).
Switching scenes as you mention appears to be a popular option too, although I prefer changing the root of the scene
instead o switching the whole thing completely.
finally, building a medium to large sized JavaFX application most likely requires an application framework of some sort. Again JavaFX core delivers no support in this regard besides the very basic application lifecycle provided by the Application
class. My suggestion is to have a look at https://github.com/mhrimaz/AwesomeJavaFX#frameworks and evaluate those options depending on your specific requirements. I'm biased but I'd suggest you to have a look at http://griffon-framework.org/. In all fairness Griffon doesn't enforce a particular strategy that can answer your question, but the recent additions to its JavaFX support should make it simpler to implement a CardPane
. Perhaps that's what we'll do for the next release :-)

- 3,236
- 18
- 28
-
Isn't a `CardPane` just a `TabPane` without the tabs actually being visible? So all this can be done pretty trivially just using a `TabPane` with a (fairly simple) custom skin. Then you get all sorts of useful selection model functionality for free. – James_D Mar 14 '17 at 12:31
-
in risk of running offtopic, @James_D you are correct, one could use `TabPane` to implement a `CardPane` however you must extend `TabPane` directly thus inhering its behavior, some of which is not desirable (insert a "Tab" on a "CardPane?). An alternative is to extend `Region` and compose a `TabPane` but then size and other properties need forwarding. At that point extending from `StackPane` would look more appealing. This shows that creating custom containers in JavaFX is less than a graceful experience given the current API :-/ – Andres Almiray Mar 14 '17 at 12:48
-
and for the record, tried once to use a `Pagination` subclass to create a `CardPane` but the whole experiment crashed hard by the inability of changing the inner behavior of mouse/key listeners due to private API (Behavior class and friends). – Andres Almiray Mar 14 '17 at 12:52
-
The functionality is actually pretty much what you want (add a new thing to the list of things that can be displayed), though I agree the names of things ("tabs") lose their semantic meaning. In this particular case, it's pretty easy to do - I wouldn't even necessarily subclass `TabPane`; just plug in a new skin. See [this](http://stackoverflow.com/questions/42569204/is-it-possible-to-reload-the-same-fxml-controller-instance/42586777#42586777) for something similar (and actually more complex). – James_D Mar 14 '17 at 12:54
-
More customizations needed indeed, compare with https://github.com/Haixing-Hu/javafx-widgets/blob/master/src/main/java/com/github/haixing_hu/javafx/pane/CardPane.java which requires less :-) – Andres Almiray Mar 14 '17 at 13:15
I think it really depends on the nature of your application. Ie., is it a single visible window that changes content depending on your activity (e.g., something like Windows calculator that can change mode), or an editor-like main window with tabbed panes, or a "tool" application like an image editor with a main view and separate child panes.
For your use case I would just switch out the root node property of your main Scene object. So you would have a "Dashboard" root node, and when you click/select something on the dashboard you just update the root node property with the new node to display.

- 962
- 2
- 10
- 29