title: JavaFX : Pass parameters while instantiating controller class
body: <p>I am working on JavaFX application right now. All my gui is in <code>.fxml</code> format and through controller class manages all GUI components. However, I have difficulties with <strong><em>instantiating the controller class</em></strong> before I load FXML loader. I was unable to find a good solution for my question from others on stackoverflow, therefore this is not a duplicate question.</p>
  
  <p>The reason why I am instantiating the controller class is that I want to <strong><em>pass some parameters</em></strong> so that these parameters will be displayed in GUI.</p>
  
  <p>I am loading an FXML file the following way:</p>
  
  <pre><code>/*
   * for Work Order button
   */
  @FXML
  private void pressWorkOrder() throws Exception{ 
      WorkOrderController wo = new WorkOrderController("ashdkjhsahd");    //instantiating constructor     
  
      Parent parent = FXMLLoader.load(getClass().getResource("/fxml/WorkOrder.fxml"));        
      Scene scene = new Scene(parent);
      Stage stage = new Stage();
      stage.setScene(scene);
      stage.setTitle("Word Order");
      stage.setResizable(false);
      stage.show();
  }
  </code></pre>
  
  <p>And here is my actual Controller class:</p>
  
  <pre><code>public class WorkOrderController implements Initializable{
  
       @FXML
       private Button summary;
       private String m,n;
  
       public WorkOrderController(String str) {
           // TODO Auto-generated constructor stub
           m = str;
       }  
  
       //for testing
       public void set(String str){
           m = str;
       }  
  
       @FXML
       public void check(){
           System.out.println("Output after constructor was initialized " + m);
       }
  
       @Override
       public void initialize(URL location, ResourceBundle resources) {
          // TODO Auto-generated method stub
       }
   }
  </code></pre>
  
  <p>And I get this Exception:</p>
  
  <pre><code>at javafx.fxml.FXMLLoader.processStartElement(Unknown Source)
  at javafx.fxml.FXMLLoader.loadImpl(Unknown Source)
  at javafx.fxml.FXMLLoader.loadImpl(Unknown Source)
  at javafx.fxml.FXMLLoader.loadImpl(Unknown Source)
  at javafx.fxml.FXMLLoader.loadImpl(Unknown Source)
  at javafx.fxml.FXMLLoader.loadImpl(Unknown Source)
  at javafx.fxml.FXMLLoader.loadImpl(Unknown Source)
  at javafx.fxml.FXMLLoader.loadImpl(Unknown Source)
  at javafx.fxml.FXMLLoader.load(Unknown Source)
  at MainController.pressWorkOrder(MainController.java:78)
  ... 57 more
  Caused by: java.lang.InstantiationException: WorkOrderController
  at java.lang.Class.newInstance(Unknown Source)
  at sun.reflect.misc.ReflectUtil.newInstance(Unknown Source)
  ... 71 more
  Caused by: java.lang.NoSuchMethodException: WorkOrderController.&lt;init&gt;()
  at java.lang.Class.getConstructor0(Unknown Source)
  ... 73 more
  </code></pre>