I have been working with JAVA
and JAVAFX
for some time now. I have come to discern certain limitations in utilizing FXML
when tasked with creating dynamic and complex applications.
My personal preference is to use JAVA code to create my layouts and views, primarily because it offers enhanced flexibility when constructing various pages
and views
.
The principal advantage of this approach lies in the ability to create custom UI elements, adaptable across multiple pages throughout the application. Examples of such reusable elements include headers, which can be employed across various pages, and product cards, which can exist in multiples within a single page.
When dealing with more complex views
, it becomes advantageous to segment the creation process using multiple methods, each specifically designated to handle a particular part of the page. This approach enhances efficiency by allowing the individual parts to be created independently before they are successfully incorporated into the window.
As such, I have the following approach that I use and I am keen to hear your perspectives on it. I welcome any advice or recommendations you might have to improve its effectiveness further.
I have the following interface, which is a base UI that all of the Views will implement:
public interface BaseUI<T> {
Node getRootNode();
void initController();
T getController();
void initUI();
}
- The
T
is a generic type which represents the class name of the controller associated with the view getRootNode()
will return the current instance of the current view.initController()
attaches the instances of the controls to the controller of the view.getController()
will return the instance of the controller of the view.initUI()
will create the controls of the view and populate it with them.
The following interface represents the base controller:
public interface BaseController<T> {
void setRootNode(T rootNode);
T getRootNode();
void initialize();
}
- The
T
represents the UI related to the controller. setRootNode()
will bind the controller to its view.initialize()
is similar in the FXML, where it will attach the logic to the controls, like adding an event listener to a button.
As you can see, this approach is based on the MVC
architecture pattern. Nevertheless, this approach occasionally induces frustration and challenges, some of which are linked to the inherent constraints of JAVA,
while others pertain to the MVC pattern itself.
For example, the body of the getNode()
and getController()
are the same, but we can not extend multiple classes in JAVA because the Views will have to extend one of the `FXML’ panes. Plus, each view has its controller, which makes the final architecture a bit complex, unlike the MVVM pattern, which binds the data between the view and the model.
I would sincerely value any advice regarding this approach. If there exist avenues for refinement or methods by which effectiveness can be further boosted, your expert input would be greatly appreciated.
Starnec is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.