When I create a project with FXML JavaFX Maven Archetype (Gluon) in Netbeans 12, I get the following hierarchy of files in the project:
└── helloworld
├── App.java
├── PrimaryController.java
└── SecondaryController.java
This is all right, but I would like to get the following structure, instead:
└── helloworld
└── controller
├── App.java
├── PrimaryController.java
└── SecondaryController.java
└── resources
└── com
└── whatever
└── fx
└── helloworld
└── fxml
├── primary.fxml
└── secondary.fxml
I follow these steps:
Add a package controller to
helloworld
package.
Add a package fxml inhelloworld
package of resourcesPut
App.java
,PrimaryController.java
, andSecondaryController.java
in the package controller.
Putprimary.fxml
andsecondary.fxml
in package fxml
Code Java
public class App extends Application {
private static Scene scene;
@Override
public void start(Stage stage) throws IOException {
scene = new Scene(loadFXML("fxml/primary"), 640, 480);
stage.setScene(scene);
stage.show();
}
public static void setRoot(String fxml) throws IOException {
scene.setRoot(loadFXML(fxml));
}
private static Parent loadFXML(String fxml) throws IOException {
FXMLLoader fxmlLoader = new FXMLLoader(App.class.getResource(fxml + ".fxml"));
return fxmlLoader.load();
}
public static void main(String[] args) {
launch();
}
}
public class PrimaryController {
@FXML
private void switchToSecondary() throws IOException
{
App.setRoot("fxml/secondary");
}
}
public class SecondaryController {
@FXML
private void switchToPrimary() throws IOException {
App.setRoot("fxml/primary");
}
- In
Module-info.java
, replace
opens com.whatever.fx.helloworld to javafx.fxml;
exports com.whatever.fx.helloworld;
with
opens com.whatever.fx.helloworld.controller to javafx.fxml;
exports com.whatever.fx.helloworld.controller;
- In
pom.xml
, replace
<configuration>
<mainClass>com.whatever.fx.helloworld.App</mainClass>
</configuration>
with
<configuration>
<mainClass>com.whatever.fx.helloworld.controller.App</mainClass>
</configuration>
- In
primary.fxml
, replace
fx:controller="com.whatever.fx.helloworld.PrimaryController">
with
fx:controller="com.whatever.fx.helloworld.controller.PrimaryController">
- In
secondary.fxml
, replace
x:controller="com.whatever.fx.helloworld.SecondaryController">
with
fx:controller="com.whatever.fx.helloworld.controller.SecondaryController">
However, after following all of the above steps, I get the following error:
Exception in Application start method
java.lang.reflect.InvocationTargetException
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:564)
at javafx.graphics/com.sun.javafx.application.LauncherImpl.launchApplicationWithArgs(LauncherImpl.java:464)
at javafx.graphics/com.sun.javafx.application.LauncherImpl.launchApplication(LauncherImpl.java:363)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:564)
at java.base/sun.launcher.LauncherHelper$FXHelper.main(LauncherHelper.java:1051)
Caused by: java.lang.RuntimeException: Exception in Application start method
at javafx.graphics/com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:900)
at javafx.graphics/com.sun.javafx.application.LauncherImpl.lambda$launchApplication$2(LauncherImpl.java:195)
at java.base/java.lang.Thread.run(Thread.java:832)
Caused by: java.lang.IllegalStateException: Location is not set.
at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2459)
at javafx.fxml/javafx.fxml.FXMLLoader.load(FXMLLoader.java:2435)
at com.whatever.fx.helloworld/com.whatever.fx.helloworld.controller.App.loadFXML(App.java:31)
at com.whatever.fx.helloworld/com.whatever.fx.helloworld.controller.App.start(App.java:20)
at javafx.graphics/com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$9(LauncherImpl.java:846)
at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runAndWait$12(PlatformImpl.java:455)
at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runLater$10(PlatformImpl.java:428)
at java.base/java.security.AccessController.doPrivileged(AccessController.java:391)
at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runLater$11(PlatformImpl.java:427)
at javafx.graphics/com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:96)
at javafx.graphics/com.sun.glass.ui.gtk.GtkApplication._runLoop(Native Method)
at javafx.graphics/com.sun.glass.ui.gtk.GtkApplication.lambda$runLoop$11(GtkApplication.java:277)
... 1 more
Exception running application com.whatever.fx.helloworld.controller.App
Command execution failed.