Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Should we use FXML in JavaFX custom controls or not?

It seems wired to ask this, I would think that using FXML to write our custom components is the obviously the right way to go.

But as we can see from ControlsFX, JFXextras and even the book 'Mastering JavaFX8 controls do not use or mention the use of FXML in custom controls.

Despite that, the official documentation to say to go on that route, and create JavaFX controls via FXML.

What is the more correct way and why ?

like image 900
David Limkys Avatar asked Nov 17 '14 22:11

David Limkys


People also ask

How do controllers work in JavaFX using FXML?

JavaFX controller works based on MVC(Model-View-Controller) JavaFX MVC can be achieved by FXML (EFF-ects eXtended Markup Language). FXML is an XML based language used to develop the graphical user interfaces for JavaFX applications as in the HTML.

What is FXML used for?

FXML is an XML-based user interface markup language created by Oracle Corporation for defining the user interface of a JavaFX application. FXML presents an alternative to designing user interfaces using procedural code, and allows for abstracting program design from program logic.

What does FXML loader do?

Class FXMLLoader. Loads an object hierarchy from an XML document.

What does FXML stand for?

FX Markup Language. In the same way that HTML is for Hypertext Markup Language, and many more acronyms that end in ML .


2 Answers

There are two kinds of custom controls in JavaFX:

  • fx:root based custom controls: These custom controls are styleable (support CSS), but are not skinnable.

    If you're an application developer, this is what you want to use most of the time.

    This kind of controls are usually application or organization specific. Additional custom skins for these controls are not needed, CSS is enough.

    Usually they just group some controls together and provide a nicer API and hide the internals.

    They are much easier to write than skinnable custom controls.

    I'm thinking of this as an alternative of the JPanel subclasses in the Swing-world.

    I suggest to break down your GUI into small self-contained fx:root based controls (high cohesion, low coupling). Use the Mediator Pattern to combine these controls (the parent control manages/ configures its child controls and listens to the events of its child controls. The child controls don't know about each other).

    You can create whole hierarchies of such controls. Breaking them down into small controls will make maintenance easier (e.g. if the layout or the user interaction has to change).

  • skinnable custom controls: skinnable custom controls are not only styleable (via CSS) but their look and feel can be changed completely by providing a custom skin.

    If you're a controls library developer then most likely you want to provide skinnable custom controls to allow the users of the library to get most out of JavaFX.

    This kind of controls are usually very basic, highly reusable controls and often (but not always) use some custom painting as well.

    They are harder to get right than fx:root based custom controls.

like image 165
Puce Avatar answered Oct 21 '22 03:10

Puce


There is no correct way. But we may break it down to this:

Do you know the amount of JavaFX elements to display before your application has started? If yes, you can build your GUI with SceneBuilder or directly in XML writing the FXML File. This is true for most GUI screen setups where the overall layout (MenuBar, ToolBar, content, sidebar, ...) is well known and does not change over time.

Now, custom components do not know how to display itself. There are many options to correspond for and most custom components are build up dynamically or can change drastically as the user changes these options. You can not write a single FXML file but instead would have to write many single files, each would build a small part of the custom component. This is not really handy to manage, so most developers choose to do it without FXML, directly in code.

And on top of that: There are many custom components which use a special layout which is not built in into JavaFX, to achieve that you have to write your own containers and wire them up with custom components to get the perfect result. In the end, custom components are built (or better: should be built) to be used inside FXML / SceneBuilder.

like image 39
eckig Avatar answered Oct 21 '22 03:10

eckig