I am working on an JavaFX app and I have decided to separate my fxml files / controllers to spread things out a bit. I have 4 main fxml files / controllers which are then included into the main fxml file and placed into a border pane (the 4 sections are numbered and outlined in red in the image link below). However, I would like to reference the controllers within the other controllers. So far, I have been using the static keyword. however, I am not really happy with that solution and was wondering if there is a cleaner way to do this. This is a snippet of how I have accomplished this so far:
Located Within File Browser Controller
@FXML
private void handleImportButtonAction(ActionEvent event) {
importCount = 0;
// Pushes the import songs task to run on the background instead of the UI Thread
Runnable importTask = new Runnable() {
@Override
public void run() {
List<Song> importSongsList = new ArrayList<>();
importSongsList = importFilesForImportButton(fileBrowser.getSelectionModel().getSelectedItems(), importSongsList);
SongDisplayWindowController.getWindowDisplaySongs().addAll(importSongsList);
//TODO: Log this to the console window and display an files that couldn't be imported
System.out.println("You succesfully imported " + importCount + " files!");
}
};
new Thread(importTask).start();
}
Located Within Song Display Window Controller
public class SongDisplayWindowController implements Initializable{
private static ObservableList<Song> windowDisplaySongs;
public static ObservableList<Song> getWindowDisplaySongs() {
return windowDisplaySongs;
}
}
Is it possible for me to use something like Spring based DI to basically make the controllers singletons and then just inject the controllers into the other controllers where I need access to their stuff?