I already searched for a solution in preview posts, but I didn't find it. I'm using javafx for some time but without the use of fxml. Now my project is getting big and I'm regretting not having used fxml before. I'm doing some test but I don't be able to run my application. The structure of my project is the following:
src|
JavaFiles|
GUIManagement| GUIManagement.java
main.java
Resources|
CSS|
FXML|MainGUI.fxml
In the following my example code: main.java:
public class MainManagement {
public static void main(String[] args) {
GUIManagement guiApp = new GUIManagement();
guiApp.runGUI();
}
}
GUIManagement.java:
public class GUIManagement extends Application implements Initializable {
// GRAPHICS ELEMENTS
@FXML private Button btnSideBoard;
@FXML private Button btnShoppingList;
@FXML private Button btnStatistics;
public void runGUI() {
Application.launch();
}
@FXML
private void test() {
System.out.println("Click");
}
@Override
public void start(Stage primaryStage) throws IOException {
AnchorPane root =
FXMLLoader.load(getClass().getResource("../../Resources/FXMLFiles/MainGUI.fxml"));
Scene newScene = new Scene(root);
primaryStage.setScene(newScene);
primaryStage.show();
}
@Override
public void initialize(URL location, ResourceBundle resources) {
btnStatistics.setText("Cambio");
}
}
MainGUI.fxml:
<?xml version="1.0" encoding="UTF-8"?>
<?import java.net.URL?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.layout.VBox?>
<AnchorPane prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/11.0.1" xmlns:fx="http://javafx.com/fxml/1" fx:controller="JavaFiles.GUIManagement.MainMenuGUI">
<children>
<VBox id="verticalPane" alignment="CENTER" layoutX="174.0" layoutY="8.0" prefHeight="486.0" prefWidth="251.0" styleClass="background">
<children>
<Label id="title" text="Shop" />
<Button fx:id="btnSideBoard" mnemonicParsing="false" styleClass="mainButton" text="Sideboard" />
<Button mnemonicParsing="false" styleClass="mainButton" text="Shopping List" />
<Button disable="true" mnemonicParsing="false" styleClass="mainButton" stylesheets="@../CSS/CSS_generic_setup.css" text="Statistics" />
</children>
<stylesheets>
<URL value="@../CSS/CSS_generic_setup.css" />
<URL value="@../CSS/CSS_main_menu.css" />
</stylesheets>
</VBox>
</children>
</AnchorPane>
Finally, the error that I obtain is:
Exception in Application start method
Exception in thread "main" java.lang.RuntimeException: Exception in Application start method
at com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:917)
at com.sun.javafx.application.LauncherImpl.lambda$launchApplication$1(LauncherImpl.java:182)
at java.lang.Thread.run(Thread.java:748)
Caused by: javafx.fxml.LoadException:
/D:/Projects/StuffManagement/out/production/StuffManagement/Resources/FXMLFiles/MainGUI.fxml:9
at javafx.fxml.FXMLLoader.constructLoadException(FXMLLoader.java:2601)
at javafx.fxml.FXMLLoader.access$700(FXMLLoader.java:103)
at javafx.fxml.FXMLLoader$ValueElement.processAttribute(FXMLLoader.java:932)
at javafx.fxml.FXMLLoader$InstanceDeclarationElement.processAttribute(FXMLLoader.java:971)
at javafx.fxml.FXMLLoader$Element.processStartElement(FXMLLoader.java:220)
at javafx.fxml.FXMLLoader$ValueElement.processStartElement(FXMLLoader.java:744)
at javafx.fxml.FXMLLoader.processStartElement(FXMLLoader.java:2707)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2527)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2441)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3214)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3175)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3148)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3124)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3104)
at javafx.fxml.FXMLLoader.load(FXMLLoader.java:3097)
at JavaFiles.GUIManagement.GUIManagement.start(GUIManagement.java:37)
at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$8(LauncherImpl.java:863)
at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$7(PlatformImpl.java:326)
at com.sun.javafx.application.PlatformImpl.lambda$null$5(PlatformImpl.java:295)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.application.PlatformImpl.lambda$runLater$6(PlatformImpl.java:294)
at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95)
at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at com.sun.glass.ui.win.WinApplication.lambda$null$4(WinApplication.java:186)
... 1 more
Caused by: java.lang.InstantiationException: JavaFiles.GUIManagement.MainMenuGUI
at java.lang.Class.newInstance(Class.java:427)
at sun.reflect.misc.ReflectUtil.newInstance(ReflectUtil.java:51)
at javafx.fxml.FXMLLoader$ValueElement.processAttribute(FXMLLoader.java:927)
... 22 more
Caused by: java.lang.NoSuchMethodException: JavaFiles.GUIManagement.MainMenuGUI.<init>()
at java.lang.Class.getConstructor0(Class.java:3082)
at java.lang.Class.newInstance(Class.java:412)
... 24 more
Someone can help me to solve this problem? Thank you very much,
Marco