• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

JavaFX Application Layout for multiple scenes

 
Michael Peremsky
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello all, I am new to JavaFX and have been through ~50 youtube tutorials on how to "build" JavaFX applications. However, every example I can find uses simple, one scene examples and/or a pop up screen. I want to create a desktop application that changes scenes depending on what menu choice is selected. If I am adding a client, I want the main scene to be an add page, if running a report, it needs to show the report page, etc.

I believe that the norm is to have a single controller for each FXML scene. But. how to I manage moving from one scene to the next? Is the Main class the location that handles all the scenes and switches between them? Any examples or links to sites with this type of information would be greatly appreciated.

 
Stevens Miller
Bartender
Posts: 1377
28
C++ Java Netbeans IDE Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Michael, maybe this tutorial will help (or, if not, maybe it will lead you to something that does).
 
Karthik Shiraly
Bartender
Posts: 1210
25
Android C++ Java Linux PHP Python
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

I want to create a desktop application that changes scenes depending on menu choice.
I believe that the norm is to have a single controller for each FXML scene. But. how to I manage moving from one scene to the next? Is the Main class the location that handles all the scenes and switches between them? Any examples or links to sites with this type of information would be greatly appreciated.


I usually do it like this:

- A main FXML with just a BorderPane (or any layout container) and MenuBar. The layout has its own Controller class (say "MainScreenController") responsible for handling menu item clicks.

- This main FXML is loaded as root node of primary Scene, and this scene is set into primaryStage. The stage's scene is never changed.

- Any UI screen is modelled as a standalone FXML (with its own Controller class). FXML gets loaded, and its root node is then added as a child node to the main FXML's BorderPane (or other layout container).

At a high level, this is how I compose views.

But I don't do that loading of UI screen FXMLs directly in the menu item event handler. That's not good design, because there may be other actions initiated outside of menu bar which may also require the exact same response. If I code all that response logic inside one layout controller's event handler, it's impossible to reuse cleanly without all kinds of reference passing hacks, resulting in a sphagetti object graph.

Instead, I do it like this:

- I have an application level controller class that is responsible for initiating the entire flow for every application use case.

- On menu item click, layout controller calls application level controller's appropriate use case method. Taking your example, it would look something like this:


- It's this applicationController that then collaborates with multiple layout controllers (including MainScreenController) and models to execute the use case.

This centralization is necessary because a use case initiated from one layout may require responses - like disabling some controls or showing a busy icon - in other layouts.


Also, every root node of a FXML layout can have its own Controller instance. It's not necessarily one Scene - one Controller. A Scene's scene graph may be composed using a tree of FXML files, each of which can have its own controller.
 
John Damien Smith
Ranch Hand
Posts: 299
15
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The question is a bit open-ended, I don't think I'll try to answer it directly.
I'll point out some resources that may (or may not) help.
If the resources end up just being confusing, then ignore them.

You could take a look at this question:
http://stackoverflow.com/questions/18619394/loading-new-fxml-in-the-same-scene
And the related "mini-framework: for swapping in and out child panes in a main FXML container.":
https://gist.github.com/jewelsea/6460130
For a slightly more involved thing, though still pretty simple, you could look at afterburner.fx:
http://afterburner.adam-bien.com
Also some basic navigation is demonstrated in:
http://stackoverflow.com/questions/13556637/how-to-have-menus-in-java-desktop-application
For a commercial solution there is something like Gluon Desktop:
http://docs.gluonhq.com/particle/1.1.0/
Which I guess is based upon Gluon Ignite dependency injection for the model data sharing aspects.
http://gluonhq.com/open-source/ignite/

In terms of navigation and handling, a lot of what you do would depend upon your interface paradigm, the two key differentiators are:
1. Web style navigation (you'll need to set your own navigation framework for providing your application with back, forward buttons and history capabilities).
2. Desktop app style navigation (act on actions, either via individual listeners on controls or on centralized actions, such as ControlsFX action handlers).
Traditional web style navigation would replace entire scenes on each navigation, though the web is moving away from that paradigm a little bit by providing support for things like ajax which update just parts of the UI on interactions rather than the entire thing. Even newer web style navigation doesn't make a lot of use of some of the things you might find in an intensive desktop application (such as drop-down menus, dockable panes, pop-up dialog boxes).

Each navigation style is valid for different types of applications and purposes and represents a bit of a continuum as there is some slight convergence between the styles as web style navigation becomes more like desktop navigation and vice versa for modern applications as desktop developers learn things from the simplicity for users of the web style navigation design and web developers gain more powerful development and technical paradigms that allow their apps to approach traditional desktop application features in some cases.
 
Michael Peremsky
Greenhorn
Posts: 15
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I realize it was a bit open ended which is why I hesitated posting, but I did not know how to narrow it down. But you guys have given me something new to look in to.

Thank you
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic