1

I'm currently trying to convert a JavaFX app to a FXML app(work requirements), and I've been running in circles for a while now. I understand the basic principle (User interface and the backend workings defined separately), but all the hoop-jumping, technicalities, workarounds, all those initialize()s, loaders, start()s, launch()s, controllers, injections and whatnot are getting a bit over my head. The app in its current state spits out a compile time error that I couldn't divine anything out of if my life depended on it, but I vaguely suspect it leads somewhere into the underlying murk of the whole FXMLLoader thing.

This is no way to work. The tutorials only explain a part of it each, and my current modus operandi is still copying over/slightly modifying bits of code I find wherever and hoping it will work somehow, rather than actually understanding the principle and taking well-reasoned steps towards a clear goal. Is there a list of everything I need to do to make a FXML app work, or some clear explanation of how it all comes together?

Sargon1
  • 854
  • 5
  • 17
  • 48
  • Maybe the second half of my answer at http://stackoverflow.com/questions/33857389/how-to-pass-data-from-java-to-fxml/33857574#33857574 will help a little. – James_D Nov 23 '15 at 13:34

1 Answers1

1

Here's a list of things required to do in the order they are typically done:

  1. Define UI in an FXML file (say ui.fxml)
  2. Write a controller class for the given FXML which contains JavaFX fields with @FXML annotation, to be injected from the above FXML.
  3. Go back to FXML file and add fx:controller="fullpackagename.ControllerName"
  4. Implement public void initialize() if necessary. This will be called after injection. Mind the method signature, must be exactly the same.
  5. Write an entry point, typically SomethingApp extends Application. In the overriden start() during Scene object construction pass the following as root node to the scene - FXMLLoader.load(getClass().getResource("ui.fxml")).

Here's a short working example. The example assumes you are familiar with the Maven directory structure. If not, then all 3 files, namely App, Controller, ui.fxml must be in the same package, for FXMLLoader to be able to locate it.

AlmasB
  • 3,377
  • 2
  • 15
  • 17