Skip to content

Can’t access controller and its methods from another controller

I’m not 100% sure how to best describe this issue but I will try my best.

I am developing a music application and have different windows within one and have loaded panes into a little in app window shown below: enter image description here

enter image description here

The bit that changes from a green button to the playlist screen is the window that has FXML files loaded into it.

I recently created a part of the application that plays songs from a button within this little window of the application but the media player is in the main application controller and needs to be here as it updates the values of the two sliders at the bottom.

I have seen something about referencing the exact instance of the controller I am using for the main application but I am unsure how to do this. I tried to create a static version of the mainAppController but this was a separate instance and therefore did not reference properly.

The only method I need to reference is the addPlayerListener one. I tried to make this static but because it dealt with the slider and so on from the FXML file that could not be made static, this did not work.

A simplified version of the controller and FXML file I need to reference from outside of the class looks like this:

public class MainAppController implements Initializable {

@FXML AnchorPane anchor;
@FXML AnchorPane window;
public static AnchorPane pane;
private boolean isPlay = false;
private boolean makeChanges = false;
public static MediaPlayer player;
PlayerUtils playerUtil = new PlayerUtils();
@FXML Slider volume, progressBar;
@FXML Label playTime, fullDuration;

String songFile;
Media songMedia;

Duration duration;
boolean updating = false;

//declaring the media player listener so that it can be removed and added
public InvalidationListener listener = new InvalidationListener()
            public void invalidated(Observable ov) {
                //if changes to the slider are being made then values will not be updated
                //as it causes interference when trying to move the slider
                if(makeChanges == false){


//this just plays or pauses depending
@FXML public void play_pause(){

//updates values of current time display (playTime) and updates the slider value
//called from the listener so constantly updating
protected void updateValues() {
        //updates values of playTime and slider values


//makes sure the player has no listeners before adding a new one
public void addPlayerListener(){

public void initialize(URL url, ResourceBundle rb) {



Sorry if this question is poorly asked, but I couldn’t think of another way of posing it.

Thank you for help in advance

Ammendment: I would actually like to access this method:

public void play_pauseFunction(){
    if(isPlay == false){
        //temp while playlists not working
        if(player == null){
            songFile = "E:\Project\Whupsh\Songs\Whenever, wherever - Shakira.mp3";
            player = new MediaPlayer(PlayerUtils.getSongMedia(songFile));
            duration = player.getMedia().getDuration();
        //adds listener to link with slider
        isPlay = true;

        isPlay = false;

This method is in the same class as all other methods



One way that worked was by having a static class holding all the controllers that would be needed to be accessed from another controller.

This is by not a particularly good way of doing things, and a better way of accessing controllers from outside another would be to pass it around as a parameter as kleopatra has said.

Passing Parameters JavaFX FXML

5 People found this is helpful