JavaFX controller class not found at Runtime
I found a solution to this :
Instead of declaring the contoller class in the FXML file, just set it programatically before loading the file like this :
FXMLLoader loader = new FXMLLoader(getClass().getRessource("myFile.fxml");
loader.setController(new MyController());
loader.load();
Hope it will help.
Error when trying to connect the controller class to javafx
Oh, this is a bit evil.
When the FXML file is loaded, as you know, it injects fields annotated with @FXML
in the controller with the elements with corresponding fx:id
attributes (the attribute value in the FXML file matching the field name in the controller).
What is little known (and undocumented) is that there are a small number of (well, two) additional values that can be injected. For example, you can do
@FXML
private ResourceBundle resources ;
to get a reference to the resource bundle provided to the FXMLLoader
. Similarly, you can get a reference to the URL provided to the FXMLLoader
with (you guessed it):
@FXML
private URL location ;
These happen automatically.
So, it looks from the stack trace that you have a ComboBox
defined in your FXML file and controller which you called location
:
@FXML
private ComboBox<String> location ;
When the FXML loader sees this, possibly before it even tries to parse the FXML file, it tries to inject the URL provided to the loader into the field, and of course fails because it's trying to assign a URL
to a ComboBox
.
To fix, just change the name of the combo box:
@FXML
private ComboBox<String> locationCombo ;
and similarly for the fx:id
attribute in your FXML file.
JavaFX Controller class variables not binding to their FXML counterparts
Your ComboBox fxml part should have fx:id
attribute set:
<ComboBox fx:id="cb_02"
This id should have EXACTLY same name as your variable in Controller class.
See tutorial for details: http://docs.oracle.com/javafx/2/get_started/fxml_tutorial.htm
Cant I move JavaFX controllers into package that is not the base package. Maven
The FXMLLoader
creates the controller via reflection, so the package containing the controller needs to be open to the javafx.fxml
module. You need
opens org.computer.ui to javafx.fxml ;
in the module-info.java
file, as stated in the stack trace.
Related Topics
Convert Latitude/Longitude Point to a Pixels (X,Y) on Mercator Projection
What Are Classes, References, and Objects
When Is the @JSONproperty Property Used and What Is It Used For
How to Compare Two Dates Without the Time Portion
Inject an Ejb into Jax-Rs (Restful Service)
Why Is the Clone() Method Protected in Java.Lang.Object
Any Way to Invoke a Private Method
Differencebetween Method Overloading and Overriding
Can Java 8 Code Be Compiled to Run on Java 7 Jvm
How to Obtain Method Parameter Name Using Java Reflection
String Concatenation in Java - When to Use +, Stringbuilder and Concat
How Is an Overloaded Method Chosen When a Parameter Is the Literal Null Value
How Does Auto Boxing/Unboxing Work in Java
What Does the Question Mark in Java Generics' Type Parameter Mean