I would like to create a custom tag/element for my fxml file but I do not want this element to be forced to inherit from a Pane, Button, TextField and so on. Is there some interface that can be implemented in my custom element that would require me to implement lets say fx() method which is required to return a Node/Region element which is supposed to be rendered in the Scene. What I mean
if something like the following is possible?
public class CustomElement implements SOME_FXML_INTERFACE {
private String myArg;
public CustomElement(@NamedArg("myArg") myArg) {
this.myArg = myArg;
}
// method that is required to be implemented by SOME_FXML_INTERFACE
// this method retuns some GUI element which actually needs to be rendered in the Scene
@Override
public Object fx() {
return new TextField(myArg);
}
}
<HBox>
<CustomElement myArg="some_argument"/>
</HBox>
All this so that I could have CustomElement who can accept custom argument in the contructor.
You can create arbitrary objects, but factory objects can only be used by nodes that support this. (Technically you could do this but it would involve using a getter returning a new instance every time it's invoked.) However you can e.g. use your custom class as
cellFactoryfor aListView.If you do not need to rely on an instance method, but are satisfied with a
staticfactory method, you can use thefx:factorytag to specify a method to create the node instance and you do not need to implement any interface to do that:More information is available in the Introduction to FXML
BTW: If your class has a
publicconstructor not taking any parameters you can create an instance of that class simply by adding a element with the name of the class to the fxml. E.g. the following fxml results in anArrayListwhen loaded; the ways you can use those instances is limited though;