title: Communication between two JavaFx controllers
body: <p>I made a structure to of Controllers and Views (fxml) to separate my code as much as I could, and I'm wondering how to communicate between 2 controllers. I mean, a controller have to call some functions of another controller to set it up to date.</p>
  
  <p>I think a schema of my current structure will be more explicit:</p>
  
  <p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Controller 1
  <br />
  &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;/&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\
  <br />
  &nbsp;&nbsp;&nbsp;fx:include&nbsp;&nbsp;&nbsp;&nbsp;fx:include
  <br />
  &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;/&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\
  <br />
  Controller2&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Controller3</p>
  
  <p>Each controller has is own fxml view.
  <br />
  - Controller 1 : a container controller which has a TabPane element with 2 tabs (each tab correspond to 1 controller)
  <br />
  - Controller 2 : a list
  <br />
  - Controller 3 : a form
  <br /><br />
  You've probably guessed that I want my form (controller 3) to automatically update my list (controller 2).
  For the moment, the form is only a "creation form", so I just want to add row in my list.
  <br />
  <br />
  I've already tried to get my Controller 2 with FXMLoader and call the functions to relaod my tableView, no success..
  <br />
  <br />
  <strong><em>Controller 1 (.java + .fxml) :</em></strong></p>
  
  <pre><code>package pappu.controllers;
  
  import pappu.core.controller.AbstractController;
  
  public class FolderController extends AbstractController
  {
  
  }
  </code></pre>
  
  <p><br />
      </p>
  
  <pre><code>&lt;?import java.lang.*?&gt;
  &lt;?import javafx.scene.control.*?&gt;
  &lt;?import javafx.scene.layout.*?&gt;
  
  &lt;VBox fx:id="view" xmlns:fx="http://javafx.com/fxml/1" xmlns="http://javafx.com/javafx/2.2" fx:controller="pappu.controllers.FolderController"&gt;
    &lt;TabPane&gt;
      &lt;tabs&gt;
        &lt;Tab text="RECHERCHE"&gt;
          &lt;content&gt;
            &lt;AnchorPane id="Content"&gt;
              &lt;children&gt;
                  &lt;fx:include source="FolderList.fxml" /&gt;  
              &lt;/children&gt;
            &lt;/AnchorPane&gt;
          &lt;/content&gt;
        &lt;/Tab&gt;
        &lt;Tab text="DOSSIER"&gt;
          &lt;content&gt;
            &lt;AnchorPane id="Content"&gt;
              &lt;children&gt;
                  &lt;fx:include source="FolderFormAdd.fxml" /&gt;  
              &lt;/children&gt;
            &lt;/AnchorPane&gt;
          &lt;/content&gt;
        &lt;/Tab&gt;
      &lt;/tabs&gt;
    &lt;/TabPane&gt;
  &lt;/VBox&gt;
  </code></pre>
  
  <p><strong><em>Controller 2 (.java + .fxml) :</em></strong></p>
  
  <pre><code>package pappu.controllers;
  
  import java.net.URL;
  import java.util.Date;
  import java.util.List;
  import java.util.ResourceBundle;
  
  import org.hibernate.Session;
  
  import javafx.beans.property.SimpleStringProperty;
  import javafx.beans.value.ObservableValue;
  import javafx.collections.FXCollections;
  import javafx.collections.ObservableList;
  import javafx.fxml.FXML;
  import javafx.fxml.Initializable;
  import javafx.scene.control.TableColumn;
  import javafx.scene.control.TableColumn.CellDataFeatures;
  import javafx.scene.control.TableView;
  import javafx.scene.control.cell.PropertyValueFactory;
  import javafx.util.Callback;
  import pappu.core.controller.AbstractController;
  import pappu.entities.Folder;
  
  public class FolderListController extends AbstractController implements Initializable
  {
      /**
       * TableView object
       */
      @FXML private TableView&lt;Folder&gt; foldersTableView;
  
      /**
       * FolderNumber column object
       */
      @FXML private TableColumn&lt;Folder, String&gt; colFolderNumber;
  
      /**
       * Person column object
       */
      @FXML private TableColumn&lt;Folder, String&gt; colPerson;
  
      /**
       * Birthday date column object
       */
      @FXML private TableColumn&lt;Folder, Date&gt; colBirthdayDate;
  
      /**
       * List of folders
       */
      private static List&lt;Folder&gt; foldersList;
  
      /**
       * Constructor
       * Will make a call to initializeFoldersList()
       */
      public FolderListController()
      {
          initializeFoldersList();
      }
  
  
      /**
       * Initialize implementation of the Initializable interface
       * 
       * @param location
       * @param resources
       */
      @Override 
      public void initialize(URL location, ResourceBundle resources) 
      {
          initializeTableColumns();
          loadData();
      }
  
      /**
       * Query the database to retrieve the folder list
       */
      @SuppressWarnings("unchecked") 
      public void initializeFoldersList()
      {
          Session session = sessionFactory.getCurrentSession();
          session.beginTransaction();
          foldersList = session.createQuery("from Folder").list();
          session.close();
      }
  
      /**
       * Initialize columns binding to folders properties
       */
      public void initializeTableColumns()
      {
          colFolderNumber.setCellValueFactory(
                    new PropertyValueFactory&lt;Folder,String&gt;("folderNumber")
                        );
          colPerson.setCellValueFactory(
                  new Callback&lt;CellDataFeatures&lt;Folder, String&gt;, ObservableValue&lt;String&gt;&gt;() {
                       public ObservableValue&lt;String&gt; call(CellDataFeatures&lt;Folder, String&gt; p) {
                           return new SimpleStringProperty(p.getValue().getFirstName() + " " + p.getValue().getLastName());
                       }}
            );
          colBirthdayDate.setCellValueFactory(
                    new PropertyValueFactory&lt;Folder,Date&gt;("birthdayDate")
                        );
  
      }
  
      /**
       * Put the folders list in the TableView object
       */
      public void loadData()
      {   
          ObservableList&lt;Folder&gt; listFold = FXCollections.observableArrayList(foldersList);       
          foldersTableView.setItems(listFold);
      }   
  }
  </code></pre>
  
  <p><br /></p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  
  &lt;?import java.lang.*?&gt;
  &lt;?import javafx.scene.control.*?&gt;
  &lt;?import javafx.scene.layout.*?&gt;
  &lt;?import javafx.scene.control.Label?&gt;
  
  
  &lt;VBox fx:id="view" xmlns:fx="http://javafx.com/fxml/1" xmlns="http://javafx.com/javafx/2.2" fx:controller="pappu.controllers.FolderListController"&gt;
      &lt;Label fx:id="lblTest"&gt;&lt;/Label&gt;
      &lt;TableView fx:id="foldersTableView"&gt;
          &lt;columns&gt;
              &lt;TableColumn prefWidth="75.0" text="N°" fx:id="colFolderNumber"&gt;
              &lt;/TableColumn&gt;
              &lt;TableColumn prefWidth="75.0" text="Personne" fx:id="colPerson"&gt;
              &lt;/TableColumn&gt;
              &lt;TableColumn prefWidth="75.0" text="Date de naissance" fx:id="colBirthdayDate"&gt;
              &lt;/TableColumn&gt;
          &lt;/columns&gt;
      &lt;/TableView&gt;
  &lt;/VBox&gt;
  </code></pre>
  
  <p><strong><em>Controller 3 (.java + .fxml) :</em></strong></p>
  
  <pre><code>package pappu.controllers;
  
  import java.io.IOException;
  import java.net.URL;
  import java.util.ResourceBundle;
  
  import org.hibernate.Session;
  import org.springframework.context.annotation.AnnotationConfigApplicationContext;
  
  import javafx.fxml.FXML;
  import javafx.fxml.FXMLLoader;
  import javafx.fxml.Initializable;
  import javafx.scene.control.TextField;
  import javafx.scene.layout.Pane;
  import pappu.core.AppFactory;
  import pappu.core.controller.AbstractController;
  import pappu.entities.Folder;
  import pappu.entities.Gender;
  
  public class FolderFormAddController extends AbstractController
  {   
      @FXML TextField folderNumber;
      @FXML TextField firstName;
      @FXML TextField lastName;
      public void submitForm() throws IOException
      {   
          Session session = sessionFactory.getCurrentSession();
          session.beginTransaction();
  
          Folder folder = new Folder();
  
          folder.setFolderNumber(folderNumber.getText());
          folder.setFirstName(firstName.getText());
          folder.setLastName(lastName.getText());
          folder.setGender(Gender.m);
  
          session.save(folder);
          session.getTransaction().commit();
              // This doesn't work.. even tried with a simple Label
          AppFactory app = new AppFactory();
          FolderListController flc = app.folderListController();
          flc.initializeFoldersList();
          flc.loadData();
      }
  }
  </code></pre>
  
  <p><br /></p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  
  &lt;?import java.lang.*?&gt;
  &lt;?import javafx.scene.control.*?&gt;
  &lt;?import javafx.scene.layout.*?&gt;
  
  &lt;VBox fx:id="view" prefHeight="216.0" prefWidth="421.0" xmlns:fx="http://javafx.com/fxml/1" xmlns="http://javafx.com/javafx/2.2" fx:controller="pappu.controllers.FolderFormAddController"&gt;
    &lt;children&gt;
      &lt;Label prefHeight="26.0" prefWidth="102.0" text="Numéro de dossier" /&gt;
      &lt;TextField prefWidth="200.0" fx:id="folderNumber"/&gt;
      &lt;Label text="Prénom" /&gt;
      &lt;TextField prefWidth="200.0" fx:id="firstName"/&gt;
      &lt;Label text="Nom" /&gt;
      &lt;TextField prefWidth="200.0" fx:id="lastName"/&gt;
      &lt;Button mnemonicParsing="false" onAction="#submitForm" text="Enregistrer" /&gt;
    &lt;/children&gt;
  &lt;/VBox&gt;
  </code></pre>
  
  <p><strong>Precisions:</strong><br>
  I made my application on this base:
  <a href="http://www.zenjava.com/2011/10/25/views-within-views-controllers-within-controllers/" rel="noreferrer">http://www.zenjava.com/2011/10/25/views-within-views-controllers-within-controllers/</a> and I use JavaFX 2 on Java JDK 7</p>
  
  <p>I feel something missing in global functioning of a JavaFX application.</p>