I am working on JavaFX application right now. All my gui is in .fxml
format and through controller class manages all GUI components. However, I have difficulties with instantiating the controller class 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.
The reason why I am instantiating the controller class is that I want to pass some parameters so that these parameters will be displayed in GUI.
I am loading an FXML file the following way:
/*
* 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();
}
And here is my actual Controller class:
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
}
}
And I get this Exception:
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.<init>()
at java.lang.Class.getConstructor0(Unknown Source)
... 73 more