3 Replies Latest reply: Aug 20, 2013 7:27 PM by jsmith RSS

    How do I create a NetBeans project with multiple JavaFX (FXML) dialogs?

    DrDBF

      I'm very new to Java and JavaFX, so forgive what is probably a question asked a thousand times, but I can't seem to find the answer.

       

      I come from a Visual Studio (C# with WPF) background. I'm used to creating a Solution then adding a Project for each dialog/window that gets included in the Solution. Each dialog/window is a class derived from Window, and I simply create an instance of that class when I need to use the dialog.

       

      What I need to do is have a main window (stage) that contains two lists of participants (a red team and a blue team).

      Then I need a separate dialog for entering the information for the participant. So when a button on the main stage is pressed ("Add Red Participant"), I want to open the ParticipantDialog. When the "OK" or "Close" button on the Participant dialog is clicked, I want the dialog to be able to return a participant object to the main stage.

      Then the main stage adds that participant to the appropriate list.

       

      What I've gathered so far is that I will be creating an FXML file for the Participant dialog (and I assume an associated controller). Then when the "Add" button is pressed, I will call an FXMLLoader to load that FXML dialog. Something like this:

       

      FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("ParticipantDialog.fxml"));
      Scene scene = (Scene)fxmlLoader.load();

      final Stage participantDialog = new Stage();
      participantDialog.setScene(scene);


      First question is: is that correct so far? Am I at least on the right track?


      Next question is: how or where do I create the FXML and controller files for the participant dialog using NetBeans? Should I create a separate project to make the participant dialog and then copy those files over, or is there a way to make the files within the parent project? And if I make the participant dialog in a separate project, what's the easiest way to access it from my main stage?


      I feel like I'm missing something simple here because this seems a lot harder than it should be.

      Thanks in advance for any advice.

        • 1. Re: How do I create a NetBeans project with multiple JavaFX (FXML) dialogs?
          jsmith

          is that correct so far?


          Yes the sample code you have is fine to launch a new dialog when your Add button is pressed .


          > I create the FXML and controller files for the participant dialog using NetBeans?


          You can edit FXML text in NetBeans, but I think that is kind of tedious.


          You might want to create the FXML using SceneBuilder.

          You can use SceneBuilder in NetBeans.


          You may want to get it working without FXML first.

          For example, use the JavaFX scene graph API, then convert your program to use FXML once you are more comfortable with the JavaFX system.

          The piecemeal approach cuts down on the amount of learning you need to do at one time.


          > Should I create a separate project to make the participant dialog and then copy those files over.


          No.


          Just use a single NetBeans project.

          You will have separate FXML files for your main stage and your participant dialog, but they will both be placed in the same NetBeans project.

           

          > is there a way to make the files within the parent project?


          In NetBeans 7.3.1, do the following:
            File | New Project | JavaFX | JavaFX FXML Application


          This will create a sample project with a sample fxml file.

          Modify the sample project files to get what you want, changing the sample fxml to your main stage fxml and adding a new participant fxml to the same directory.

          • 2. Re: How do I create a NetBeans project with multiple JavaFX (FXML) dialogs?
            DrDBF

            Thanks.

            I got it working, but now I have a follow up question.

            How do I access variables in the associated controller. For example, one public boolean is simply isOK meaning the user selected OK to adding the participant (rather than Cancel to NOT add). How do I get access to isOK?

             

            Here is my code:

             

                Parent dlgRoot = FXMLLoader.load(getClass().getResource("SiRGCombatantDialog.fxml"));
                Scene scene = new Scene(dlgRoot);
                FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("SiRGCombatantDialog.fxml"));
                SiRGCombatantDialogController controller = fxmlLoader.getController();
                final Stage participantDialog = new Stage();
                participantDialog.setScene(scene);
                participantDialog.showAndWait();
              
                boolean isOK = false;
                isOK = controller.isOK;
                if (isOK)
                    Dialogs.showInformationDialog(participantDialog, "Message", "Header", "Title");

             

            The line with fxmlLoader.getController() is returning null. Other than that, it is all working OK (although obviously since controller is null, it crashes on the line where I set isOK to controller.isOK). So clearly I am missing something as far as getting at the controller.

            How do I get access to the controller class?

             

            Thanks.

            David.

            • 3. Re: How do I create a NetBeans project with multiple JavaFX (FXML) dialogs?
              jsmith

              > I got it working

               

              Great!!

               

              > How do I access variables in the associated controller.

               

              This is the most common question related to FXML.

               

              Don't use a static load method (they are evil), create an FXMLLoader instance and use an instance load method instead and all should be good.

               

              The short answer is either:

                1. Create a controller and set it on the FXMLLoader instance *before* calling load OR

                2. Load an FXML using the FXMLLoader instance, then get the controller from the loader *after* loading the FXML.

               

              And in both case add appropriate access methods to your controller to get or set the state of the controller.

               

              More complete answers are in these links:

              1. dependency injection - Passing Parameters JavaFX FXML - Stack Overflow
              2. javafx 2 - Multiple FXML with Controllers, share object - Stack Overflow
              3. https://blogs.oracle.com/acaicedo/entry/managing_multiple_screens_in_javafx1