0

I give you an example of code wich don't work. before going further. i d like an explenation of what's wrong with my class.

the code is in three part: MainApp with start method, controleur with initialize method, and a blank layer fxml configured in order to be controlled by "controleur.java"

here is my example, MainApp.java:

    public MainApp MainApp;

    public controleur mesDonnees;
    private Stage primaryStage; 
    private AnchorPane rootLayout;
    public boolean passepasse;


    @Override
    public void start(Stage primaryStage) throws IOException {
         this.primaryStage = primaryStage;

        rootLayout = new AnchorPane();
        MainApp = new MainApp();
        mesDonnees = new controleur();
        mesDonnees.setMainApp(this);
        this.passepasse =true;
        System.out.println(passepasse);

        CreationFichier();
        }

    public void CreationFichier() throws IOException{
         // Load the fxml file and create a new stage for the popup dialog.
            FXMLLoader loader = new FXMLLoader();
            loader.setLocation(MainApp.class.getResource("FXML.fxml"));
            AnchorPane page = (AnchorPane) loader.load();
            // Create the dialog Stage.
            Stage dialogStage = new Stage();
            dialogStage.setTitle("Création d'un Cannevas");
            dialogStage.initModality(Modality.WINDOW_MODAL);
            dialogStage.initOwner(primaryStage);
            Scene scene = new Scene(page);
            dialogStage.setScene(scene);
            controleur controller = loader.getController();
            controller.setDialogStage(dialogStage);
            controller.setMainApp(this);


            dialogStage.showAndWait();       
    }

    public boolean getPassePasse(){
        return this.passepasse;
    }
    public static void main(String[] args) {
        launch(args);

    }
}

and the controller controleur.java

package MainApp;

import javafx.fxml.FXML;
import javafx.stage.Stage;
public class controleur {
    private MainApp MainApp =new MainApp();
    private Stage dialogStage;



         @FXML
        private void initialize() {

            System.out.println(MainApp.getPassePasse());

         if (false){  
         /* INSTRUCTIONS depend if passe passe is true or false;*/
        }
    }


    public void setDialogStage(Stage dialogStage) {
        this.dialogStage = dialogStage;
    }

    public void setMainApp(MainApp mainApp) {
        this.MainApp = mainApp;

    } 

}

results :
System.out.println(passepasse) give true

but

System.out.println(MainApp.getPassePasse()); give false.

It is a question of inilization I guess, but what is the good method to avoid this?

  • Possible duplicate of [Passing Parameters JavaFX FXML](http://stackoverflow.com/questions/14187963/passing-parameters-javafx-fxml) – fabian Oct 05 '16 at 08:11
  • What's the question? `initialize()` is invoked during the call to `FXMLLoader.load()`, so you are calling `MainApp.getPassePasse()` before you call `setMainApp(...)`. – James_D Oct 05 '16 at 11:54

0 Answers0