Question

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?

Était-ce utile?

La solution

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 !

Autres conseils

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
    }
}
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top