0

I'm trying to create a simple app which read content of .xml files and display it into JavaFX window as a slideshow.

However, it is not possible to use attributes in start method? Are they set?

Source code

public class App extends Application {

    private ArrayList<LomDocument> filesList = new ArrayList<LomDocument>();

    private int index = 0;


    public void start(Stage primaryStage) {
        ...

        // Fill file list
        File directory = new File("src/main/resources/lom");
        for (File file : directory.listFiles()) {
            LomDocument lom = new LomDocument(file.getAbsolutePath());
            this.filesList.add(lom);
        }

        System.out.println(this.filesList.size()); // Return 6

        Scene scene = new Scene(root);
        primaryStage.setScene(scene);
        primaryStage.show();
    }

    public void nextSlide() {
        System.out.println(this.filesList.size()); // Return 0

        if(this.index < this.filesList.size()) {
            this.index++;   
        }
    }

 }

With index I know on what slide I am.

As it mentionned on the code, when I click on the button nextSlide, this.filesList.size() return 0 whereas in start method size is 6.

Can you help me to understand why value is not the same?

Thank's for help!

Royce
  • 1,557
  • 5
  • 19
  • 44
  • 1
    from where do you call the methods? Any chance you have more than one instance of `App`? – luk2302 Feb 10 '18 at 16:48
  • `nextSlide` is an event on a button into the window. I think it no exists more than one instance of `App`, at least I hope so ... – Royce Feb 10 '18 at 16:50
  • Did you set `controller="App"` in your `FXML`? – Timothy Truckle Feb 10 '18 at 16:52
  • @TimothyTruckle in the controller class ? Yes. Especially than buttons `next` and `previous` works. – Royce Feb 10 '18 at 16:55
  • 2
    If you specified the controller with the `controller="App"` attribute in the `FXML` file JavaFX will create a second instance of `App`. – Timothy Truckle Feb 10 '18 at 16:57
  • 2
    Don't use the application class as the controller. The `FXMLLoader` creates a new instance of the controller class each time you call `load()`. – James_D Feb 10 '18 at 16:58
  • I see, but if I remove `tp2.App` from `fx:controller` attribute into my `.fxml` file I got this error : `javafx.fxml.LoadException: No controller specified.` – Royce Feb 10 '18 at 17:05
  • 3
    The point is you should create a *different* class to use for the controller. – James_D Feb 10 '18 at 17:08
  • @James_D ok, and this class will be instanciate when I start the application or I must instanciate it into start method? Thank's! – Royce Feb 10 '18 at 17:10
  • 1
    Read my previous comment. "The `FXMLLoader` creates a new instance of the controller class each time you call `load()`" – James_D Feb 10 '18 at 17:11
  • @James_D my bad. Thank's a lot ;) – Royce Feb 10 '18 at 17:15
  • see here how to pass constructor parameters to a controller class: https://stackoverflow.com/questions/14187963/passing-parameters-javafx-fxml (section *Setting a Controller on the FXMLLoader*) – Timothy Truckle Feb 10 '18 at 18:59
  • Indeed, that can help me in the future :D – Royce Feb 10 '18 at 19:28

0 Answers0