3 Replies Latest reply on Dec 6, 2011 8:19 PM by 903683

    What's the best way to organize JavaFX 2.0 code?

      In every example I have seen yet almost all of the GUI code is written in the start(Stage stage) method of the main class. Is this the best approach, or is it OK to subclass the Stage class and make your own thing? If the latter is OK, wouldn't the performance suffer because of lot of Stages?
        • 1. Re: What's the best way to organize JavaFX 2.0 code?
          I wouldn't sub-class Stage. I'd stick to having one stage with one scene in it, then put a Pane at the top level (like a StackPane or BorderPane) that acts much like the contentPane does in a Swing JFrame, i.e. add your views to it, swapping them in and out as needed. Each of your views would probably then extend a Pane of some type, and have a controller (and optionally a model) as part of their MVC or MVP design.

          I'd also recommend using a GUI factory to wire up all your views, controllers and models (and other resources), using dependency injection (which just means you pass in stuff to your class via a setter instead of newing it yourself inside the class - you can use Guice or Spring, but you don't have to it, if you want just code it by hand). This leads to nice modularised code among other things.

          My JFX Flow project is based around this idea and provides a framework for making this sort of thing a bit easier: http://www.zenjava.com/jfx-flow/

          If you don't want to use the framework directly you can have a look at the 'showcase' codebase for an example of how I would structure an application:
          Source code: http://code.google.com/p/jfxflow-samples/source/browse/trunk/showcase/
          Demo: http://zenjava.com/demo/jfxflow/showcase/

          The JFX Flow source code might be a little less intuitive to follow, but if it helps: http://code.google.com/p/jfxflow/source/browse/

          Hope that helps,
          1 person found this helpful
          • 2. Re: What's the best way to organize JavaFX 2.0 code?
            I'd try to seperate view and logic into two separate classes. (see various patterns like Presentation Model, MVP, MVC).

            I subclassed Stage, but only for sub-windows, which popup up, when you click a menu button.

            Concerning your "GUI Factory": I wonder how to manage all your views and stages. E.g. if I open a new window (stage) from two different places, I need a manager, that manages this one window instance, because I think it is bad practice to have two instances for the same window.
            Is Spring appropriate for this?
            You would instantiate all Spring-managed classes, even if you don't need the instance.
            1 person found this helpful
            • 3. Re: What's the best way to organize JavaFX 2.0 code?
              Thank you very much for your answers.

              I will be using MVC. I heard already of dependency injection too, but only in connection with frameworks you named (Guice, Spring). I didn't know I was using it all the time with the setter methods. Thanks for the clarification of that too!

              I will check your project in the next few days, I'm really anxious about some great examples. If any of you could list some other great examples I would be really grateful.