1

What are the pros and cons of using FXMLs or not using FXMLs for developing JavaFX applications?

For developing enterprise JavaFX application which approach should one follow?

Dave Jarvis
  • 30,436
  • 41
  • 178
  • 315
Angom
  • 721
  • 1
  • 11
  • 27
  • See related question [Java FX: declarative vs procedural](http://stackoverflow.com/q/16161781/1155209), which also discusses this topic. – jewelsea Aug 11 '16 at 08:20

2 Answers2

4

FXML Cons: It takes slightly longer to load and display.

FXML Pros:

  • Rapid scene development / mock up using Scene Builder.
  • FXML is not a compiled language; you do not need to recompile the code to see the changes. Just reload the FXML file.
  • It provides a clear separation of GUI from logic/controller.
  • So you can have different versions of a scene/view using the same controller. This is handy for demo's for instance.
  • The content of an FXML file can be localized as the file is read.

Definitely use FXML in enterprise apps !

Dave Jarvis
  • 30,436
  • 41
  • 178
  • 315
Jurgen
  • 2,138
  • 12
  • 18
  • So, is there any recommendation to use FXMLs always? – Angom Jan 07 '14 at 08:17
  • @Ang In general use FXML. For a very simple view like a popup containing a textfield and a button, you maybe don't have to use FXML. – Jurgen Jan 07 '14 at 10:21
  • Designing a scene and placing controls using code can become messy as the complexity grows. Scenebuilder provides a very interactive way to place your controls on the scene and preview it immediately. – Sudip Saha Jan 07 '14 at 17:58
  • @Angom Number 3 of Jurgen Pros is really important. SceneBuilder can be a strong pro, still personally I don't feel the need to use it: many people use a declarative language for GUI without a WYSIWIYG tool. Bear in mind that the Separation of Concerns is always important in software. – zenbeni Jan 14 '14 at 11:14
  • 5
    I disagree with the recommendation to "definitely" use FXML in enterprise apps. I started writing my app with FXML, but soon gave up on it because it is too constraining. My main objection is that you can't use generic controllers. You can live without them, but it makes things much harder and code less elegant. Also, for large forms, it takes more than "slightly longer" to load. FXML file needs to be parsed and object graph created using reflection. And interestingly, now that I've written some helper methods, I can actually build forms more quickly from code than using Scene Builder. – Nikša Baldun Jan 15 '14 at 22:38
  • @Nik Thanks for the comment. I'm intrigued by your use of generic controllers, can you provide a link please to show an example ? (Not sure if this is relevant but FXMLLoader can be assigned a separate controller or even a controller factory.) – Jurgen Jan 16 '14 at 06:43
  • @Jurgen For example. if you have a form that displays a list of entities, you don't want to create a separate controller for every class in your data model. You'll most likely create generic ListController class, but you can't specify parameterized class in FXML file. You can assign it at runtime, like you said, but the you don't get syntax checking. Bottom line: if you write for desktop only, slowness of FXML probably won't bother you, but expect that sometimes you will have to find workarounds for its restrictions. Personally, I just think it isn't elegant enough. – Nikša Baldun Jan 16 '14 at 09:03
  • @Nik Thanks. I'm not clear on the syntax checking though, doesn't the following provide it: listCtrl = new ListController(); and then you do stuff with the controller ? What about having an AbstractListController that the fxml controller then extends ? – Jurgen Jan 16 '14 at 09:52
  • @Nik Maybe it depends on what you are trying to do with generic controllers. I have a generic controller for interacting with classes that implement a particular interface and it works very well. The controller UI is in FXML and the parameterized controller extends VBox for use in other UI elements. (e.g. class SelectTypeDialog extends VBox ...) – chooks Feb 25 '14 at 15:41
3

I would add two contra to Jurgens list.

If you are working with FXML instantiation of your view is sort of inconvenient. At least from my point of view.

Node explorer = new MyExplorerWidget();

or

Node explorer = cdicontainer.newInstance(MyExplorerWidget.class);

is more pleasant than

FXMLLoader loader = new FXMLLoader(getClass().getResource("com.mycompany.some.very.long.name.MyExplorerWidget.fxml"),explorerwidgetresouces);//Of course we want our app internationalized
Node explorer = loader.load();

Another point is that FXML is static. If you want to generate your UI at run time depending on some model you will write UI code anyway. I ended up with useless fxml files like this PropertyGrid.fxml

<AnchorPane xmlns:fx="http://javafx.com/fxml/1" xmlns="http://javafx.com/javafx/8" fx:controller="PropertyGridController">
    <children>
        <VBox fx:id="vbox" layoutX="63.0" layoutY="-28.0" prefHeight="172.0" prefWidth="163.0" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0" />
    </children>
</AnchorPane>

And the PropertyGridController.

public class PropertyGridController{

    @FXML
    VBox vbox;

    ....

    public void setModel(PropertySheet model){
        //.... tons of code to generate the actual property grid and add it to the view
    }
}
Jurgen
  • 2,138
  • 12
  • 18
FuryFart
  • 2,304
  • 4
  • 27
  • 43