title: Load FXML file on tableview row selection
body: <p>Please, I would like to load an FXML file on clicking on a tableview row (row selection) and retrieve the values of the row from a database and setting the values on the labels of the FXML file.</p>
  
  <p>The examples I've seen so far doesn't implement thus.</p>
  
  <p>I would appreciate if anybody can help on FXML file loading.</p>
  
  <p><strong>My current code:</strong></p>
  
  <pre><code>package application;
  
  import java.io.IOException;
  
  import javafx.application.Application;
  import javafx.beans.value.ChangeListener;
  import javafx.beans.value.ObservableValue;
  import javafx.collections.FXCollections;
  import javafx.collections.ObservableList;
  import javafx.fxml.FXMLLoader;
  import javafx.stage.Stage;
  import javafx.scene.Group;
  import javafx.scene.Parent;
  import javafx.scene.Scene;
  import javafx.scene.control.TabPane;
  import javafx.scene.control.TableColumn;
  import javafx.scene.control.TableView;
  import javafx.scene.control.cell.PropertyValueFactory;
  import javafx.scene.paint.Color;
  
  public class Main extends Application {
  ObservableList&lt;Person&gt; persons = FXCollections.observableArrayList();
  @Override
  public void start(Stage primaryStage) {
  Parent root =         FXMLLoader.load(getClass().getResource("personsTable.fxml"));
      Scene scene = new Scene(root);
      newStage.setScene(newScene);
      newStage.show();
  try {
           Class.forName("org.apache.derby.jdbc.ClientDriver");
          conn=DriverManager.getConnection("jdbc:derby://localhost:1527/Employee","Conduct","ccb");
  
          String rest="select *from Employee";
          pstmt=conn.prepareStatement(rest);
          rs=pstmt.executeQuery();
  
  
  
          while(rs.next()){
              persons.add(new Person(
                      rs.getString("name"),
  
  
              ));
  
              staff.setCellValueFactory(new PropertyValueFactory&lt;&gt;("name"));
  
              personsTable.setItems(persons);
              personsTable.getSelectionModel().selectedItemProperty().addListener(new ChangeListener&lt;Person&gt;() {
  
          @Override
          public void changed(ObservableValue&lt;? extends Person&gt; observable, Person oldValue, Person newValue) {
  
              // Create a new FXML loader 
              FXMLLoader loader = new FXMLLoader(getClass().getResource("OnSelection.fxml"));
              try {
                  // Load the another FXML file 
                  Parent newParent = loader.load();
                  OnSelectionController subController = loader.getController();
                  // Set the String property
                  // If you want to use data from the current selection: newValue contains the currently selected Person
                  // TODO: Get value from DB
                  subController.textToDisplay.set(newValue.getName());
  
                  // newParent contains the root of your other FXML file, do anything that you want to do with it (e.g. add to the current node graph)
                  // Now I just simply open it in a new window
                  Stage newStage = new Stage();
                  Scene newScene = new Scene(newParent);
                  newStage.setScene(newScene);
                  newStage.show();
  
                  } catch (IOException | SQLException ex) {
                      Logger.getLogger(ViewController.class.getName()).log(Level.SEVERE, null, ex);
                  }
              });
  }
  
           } catch (SQLException | ClassNotFoundException ex) {
          Logger.getLogger(ViewController.class.getName()).log(Level.SEVERE, null, ex);
      }
  
  }
  </code></pre>
  
  <p>}  </p>