Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to locate fxml from another package?

Tags:

javafx

fxml

I have created a simple JavaFX application.

enter image description here

It has two packages the main class is JFXTest2.java is in good package and the fxml and it's controller are in JFXTest2 package.

now the problem is that i can not load the fxml in the main class. I tried loading the fxml like this:

Parent root = FXMLLoader.load(getClass().getResource("jfxtest2.Screen.fxml"));

and

Parent root = FXMLLoader.load(getClass().getResource("jfxtest2/Screen.fxml"));

and also

Parent root = FXMLLoader.load(new URL("/jfxtest2/Screen.fxml"));

but none of them worked.So how should i load the fxml from JFXTest2 package in the the JFXTest2 class which is the main class or application class.

like image 433
Cid Avatar asked Apr 07 '14 16:04

Cid


People also ask

How do I load a FXML file from another package?

java is in good package and the fxml and it's controller are in JFXTest2 package. Parent root = FXMLLoader. load(new URL("/jfxtest2/Screen. fxml"));

How do I add FXML to another FXML?

The <fx:include> tag can be used to include one fxml file into another. The controller of the included fxml can be injected into the controller of the including file just as any other object created by the FXMLLoader . This is done by adding the fx:id attribute to the <fx:include> element.

Can two FXML have same controller?

It's possible to use the same controller class for multiple FXML files, but your code will be really hard to follow, and it's a very bad idea. (Also note that using the fx:controller attribute, you will have a different controller instance each time you call FXMLLoader.


1 Answers

Try

Parent root = FXMLLoader.load(getClass().getResource("/jfxtest2/Screen.fxml"));
like image 82
James_D Avatar answered Sep 20 '22 14:09

James_D