I used Swing for years and now I am moving to JavaFX. Despite many similarities, I'm confused about some topics, such as how to develop larger applications which involve many scenes/stages effectively. In Swing the most used approach was inheritance, e.g by subclassing JPanel class or other Swing components. From what I saw until now, it seems that in JavaFX this is not the common pratice. Although it is possible to subclass Stage and Scene classes, it seems it's not recomended.
But I also noticed that, especially in cases of a complex GUI, I end up with my main class (the one containing the start
method) becoming an enormous cluster of hundred of lines of code. Such a code seems pretty hard to debug and mantain, but probably I am using a wrong approach. While in Swing this could be avoided in some ways, for example by subclassing some components and reusing them, is there any similar design technique that may help me to break up my JavaFX app in more classes?
Asked
Active
Viewed 524 times
0

kernel_panic
- 119
- 11
-
You can take a look at FXML, the Scene Builder the controllers behind. – pzaenger Jul 18 '17 at 17:58
-
1I've been extending a StackPane and using it as a 'scene manager' with loadscreen, unloadscreen, setscreen type methods. Works great. – Eric Jul 18 '17 at 18:06
-
1It sounds like you have misunderstood the role of the `Application` class - which essentially encapsulates the life cycle of the entire application - and the `start` method (which is invoked when the application starts). There should not be very much code in the `Application` class, and there is no real need to subclass the node or pane classes to write a complex application (though you can if you like). Have a look at the structure in my answer to https://stackoverflow.com/questions/32464698/java-how-do-i-start-a-standalone-application-from-the-current-one-when-both-are – James_D Jul 18 '17 at 18:08
-
@James_D I may have misunderstood it. Unfortunately, most of examples of applications embed all the code in the start method or in the Application subclass. Your comment enlighted me a bit, if you know a link were a well-written application's source code is posted that would be great – kernel_panic Jul 18 '17 at 18:56
-
1Take a look at the structure of some existing applications, such as [scenebuilder](http://ladstatt.blogspot.com/2014/01/home-made-javafx-scenebuilder.html) and the [makery javafx tutorial](http://code.makery.ch/library/javafx-8-tutorial/). – jewelsea Jul 18 '17 at 20:16
-
1There are also various third party JavaFX app frameworks that handle stuff like dependency injection and imply a structure on applications, which you could google. Though, looking at third party links might confuse you more than not at this stage, so I'd advise sticking to the previous links James and I provided for now. Also, note that the trouble with "most examples" in this regard is that they are built just to illustrate a single minor point and they are not large apps, so by their nature they are structured differently than even a medium sized app. – jewelsea Jul 18 '17 at 20:25
-
See the `FXMLLoginDemoApp` in *Ensemble* among the samples cited [here](https://stackoverflow.com/a/31761362/230513). – trashgod Jul 19 '17 at 01:55
1 Answers
1
I would take a look at this tutorial by Oracle which walks you through building a multi-screen javafx application.
The code for this tutorial can be found here Acaicedo GitHub
It follows MVC (Model View Controller) where FXML files are views, associated with unique controllers written in java. This framework adds an extra controller that allows navigation between screens (ie. shifting the show content to a different controller and view).

Eli
- 76
- 1
- 9