I have BorderPane
, which is divided to 3 AnchorPane
s that are changing time to time. Is it possible to get full .fxml
file name like 'MainScreen.fxml'? I can get AnchorPane
with System.out.println(getRootLayout().getCenter());
, but I can't figure out how to get it's name.
It's just I need to call from main class for method that is located in one of my controllers. But I want to call it only if certain fxml file for that controller is currently in use.
In short: if MainScreen.fxml is in use I will call it's MainScreenController's method, if not, then I won't.
Based on your question I assume that each AnchorPane is loaded from a different FXML file and that you keep a reference to their controllers in your application.
A simple solution would be to put the file information into the controller.
You can either do this manually...
URL location = getClass().getResource("MainScreen.fxml");
FXMLLoader fxmlLoader = new FXMLLoader(location);
// ... more fxmlLoader configuration ...
fxmlLoader.load();
// assuming your own Controller class has a setLocation method
Controller c = (Controller) fxmlLoader.getController();
c.setLocation(location);
... or rely on the FXMLLoader.
// your own Controller class
public class Controller
{
// the URL will be automatically injected by the FXMLLoader
// careful: the name of the field must be 'location'
@FXML
private URL location;
// ...
}
Then you can simply add a getLocation()
method to your controller to check from which file it was loaded.
However, there is probably a better solution for your underlying problem that does not require checking for a specific file name.
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments