-1

I try to developp an application and I work on Eclipse.

First I worked on four files :

  • Main.java, that launch the interface by using the sheet1.fxml file

  • MyController.java, that declares the button and the anchor of the fxml file sheet1. That file implements an action event : to go on a second interface (sheet2.fxml) after clicking on the button of the first fxml file (that is sheet1).

Now I want to work on the second interface, sheet2.fxml. I'd like to :
- add text based on a count of files in a folder
- create buttons in order to go on a third interface

But my question is how could I do ?
I tried to create a second controller to make the declaration of the Text nbExcel and buttons then build the relation with sheet2.fxml file but I don't see the trick.
How to associate events that run the components located on that sheet2 and that new controller ?

I began to do that on "myController2" :

       public class myController2 extends Application {

        // How and where to associate that controller with the fxml file "sheet2" ?

      public void start2() throws IOException{
      Stage primaryStage2 = new Stage();

      Parent root = FXMLLoader.load(getClass().getResource("sheet1.fxml"));
      Scene scene = new Scene(root,400,400);

scene.getStylesheets().add(getClass().getResource("application.css").toExternalForm());
      primaryStage2.setScene(scene);
      primaryStage2.show();
      }

       @FXML
       // declaration text in order to count files in folder in sheet2 interface
       private Text nbExcel;

       // declaration action buttons in sheet2 interface
       // To do


       // 1 - INITIALISATION
       public void initialize(URL location, ResourceBundle resources) {

           File a = new File("C:/Controles/Excel");
           int b = 0;
           for (File file : a.listFiles()) {
           if (file.isFile() && (file.getName().endsWith(".xlsx") )) {
              b++;
            }
           }
           nbExcel.setText(Integer.toString(b));
       }

Any help would be really appreciated.
Thank you !

Julien
  • 699
  • 3
  • 14
  • 30
  • Please write [a minimal, complete, and verifiable example](https://stackoverflow.com/help/mcve). I've not read your code because its too long and I'm sure 90% of it is unnecessary clutter. – Michael Aug 16 '17 at 12:11
  • Moreover the part you seem to be needing help with is the part you *didn't* post, but just tried to describe. Descriptions of code are rarely useful. "Should I create another controller class for each sheet?": yes. Each FXML file should have its own controller class. – James_D Aug 16 '17 at 12:14
  • Ok I'm sorry.. I made edits on my first post. Thank you very much for your help – Julien Aug 16 '17 at 12:32

1 Answers1

0

In order to connect the fxml file and create the new scene you could do something like this:

        Stage primaryStage = new Stage();

        Parent root = FXMLLoader.load(getClass().getResource("/application/sheet2.fxml"));
        Scene scene = new Scene(root,400,400);
        scene.getStylesheets().add(getClass().getResource("application.css").toExternalForm());
        primaryStage.setScene(scene);
        primaryStage.show();

EDIT: To confirm this bit of code should be in the controller class that you have shown in your question.

KobiF
  • 60
  • 1
  • 12
  • Thank you for your help. I put your snippet into a public void (see my edits) but it does not work. The new controller does not seem to be associated with the fxml file.. Thank you – Julien Aug 16 '17 at 12:51
  • Hmmm. Im not really sure what the issue is then. Maybe you need to connect them in SceneBuilder if you use that. @Julien – KobiF Aug 16 '17 at 12:56
  • I pasted the content of "View - Show Sample Controller Skeleton" in **myController2** but unfortunately same result.. – Julien Aug 16 '17 at 13:03