I put this in Application start
File userPath = new File(System.getProperty("user.dir"));
for (String s : userPath.list())
if (s.endsWith(".css")){
MenuItem mi = new MenuItem(s);
//using already made menu in my prog
controller.mnuEdit.getItems().add(mi);
mi.setOnAction((e)->{
//always adding and removing from position 1
//I already have a style sheet set at 0
scene.getStylesheets().remove(1);
try {
scene.getStylesheets().add(1,new File(
System.getProperty("user.dir")+
System.getProperty("file.separator")+s)
.toURI().toURL().toExternalForm());
} catch (MalformedURLException ex) {
}
});
}
Just in case you don't have a ref to controller, I load my fxml like this.
try {
FXMLLoader fxmlLoader = new FXMLLoader();
fxmlLoader.setResources(ResourceBundle.getBundle("myprog/language/Base", Locale.getDefault()));
rootScene = fxmlLoader.load(this.getClass().getResource("Base.fxml").openStream());
controller = (BaseController) fxmlLoader.getController();
} catch (IOException ex) {log.severe(ex.getMessage());}