JavaFX FXML Controller Class accessing function in main class?


Question

I'm using JavaFX to make a login app that switches to a new scene when the user clicks a button. When in the controller class, how do I access my ReplaceSceneContent() function in the main application? This is the code I have so far, which gives an error message:

public class MainScreenController implements Initializable {


private FXMLStephensPetrochko application; 
public void setApp(FXMLStephensPetrochko app){
    this.application = app;
}
@Override
public void initialize(URL url, ResourceBundle rb) {
}
public void Pressed (ActionEvent e){
    try{
        application.replaceSceneContent("LoggedIn.fxml");
    }catch (Exception exc) {
        exc.printStackTrace();
    }
    System.out.println("Button Pressed!");

}
}
1
2
12/30/2012 8:45:28 PM

Accepted Answer

public void setApp(FXMLStephensPetrochko app){
    this.application = app;
}

this setter method doesn't inject main app instance automatically . To access methods's defined in main from controller class see example code here

3
5/23/2017 11:59:20 AM

Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow
Icon