I'm writing an app that will have many data entry windows, each of which has a label for system messages.
I have a GenUtil class for common methods, one of which sets the system message in the controller that called the method.
Setting a system message works if I pass the controller reference to the method ie.
Create a reference to the data entry window controller when the FXML is loaded:
deWindowController = loader.getController();
In the data entry window controller:
genUtil.setSystemMessage(this);
In GenUtil:
public void setSystemMessage(FXMLDEWindowController deWindowController) {
deWindowController.lblSysMsg.setText("setting the message");
}
However, the setSystemMessage method will be called from many FXML controllers I can't figure out how to "genericise" this process ie.
1) What goes in the method's parameter:
public void setSystemMessage(**<WHAT_GOES_HERE?>** controllerRef) {
2) Assuming the system message label IDs are all lblSysMsg, can I use controllerRef in the same way as before to set a message label?
I could include references to all of the controllers in the GenUtil class and in each of the controllers, pass a string containing the data entry window name when I call the setSystemMessage method. That way I could manually work out which controller to use. However, I'm trying to avoid that.
Can anyone help please?
I'm using JavaSE8 and NetBeans8.2.
You should not provide direct access to fields. This would allow a user of the class to do with the field including setting it to null
or modifying properties other than the text
property.
Declare a setSystemMessage
method in a common supertype of the controller. If all of the controllers contain the same field, a abstract class would be a good choice to avoid repetition but you could also use a interface.
Use this supertype as the type of the controllerRef
parameter:
public void setSystemMessage(SuperType controllerRef) {
controllerRef.setSystemMessage("setting the message");
}
public abstract class SuperType {
@FXML
private Label lblSysMsg;
public void setSystemMessage(String message) {
lblSysMsg.setText(message);
}
}