Javafx - Weird error in trying to create new window

Viewed 64

I can't even begin to describe the error I'm getting but I'll try. This project uses JavaFX.

Basically what I'm trying to do is to open a new Window, and my first time trying to create a Window came down to this:

public class Main extends Application {

public static void main(String[] args) {
    launch(args);
}

@Override
public void start(Stage stage) throws Exception {
    try {
        URL fxmlLocation = this.getClass().getResource("/com/bitdhelper/pages/MainMenu.fxml");
        FXMLLoader fxmlLoader = new FXMLLoader(fxmlLocation);
        Parent root1 = fxmlLoader.load();

        stage.setScene(new Scene(root1));
        stage.show();
    } catch (Exception e) {
        e.printStackTrace();
    }
}}

And it worked as expected! But I wanted to have a class with its own methods to create a Window, so that there wouldn't be boilerplate code every time I need a new Window to be created (there will be a lot of Windows in this app). So I've created a class called ScreenFXController with the following code:

public class ScreenFXController {

public ScreenFXController(String resource, String title, boolean resizable, boolean isOnFocus, boolean isOnTop){
    createScreenFX(resource, title, resizable, isOnFocus, isOnTop);
}

public void createScreenFX(String resource, String title, boolean resizable, boolean isOnFocus, boolean isOnTop){
    try {
        FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource(resource));
        Parent root1 = fxmlLoader.load();
        Stage stage = new Stage();

        stage.initStyle(StageStyle.DECORATED);
        stage.setTitle(title);
        stage.setResizable(resizable);
        stage.setAlwaysOnTop(isOnTop);
        if (isOnFocus){
            stage.initModality(Modality.APPLICATION_MODAL);
        } else {
            stage.initModality(Modality.WINDOW_MODAL);
        }

        stage.setScene(new Scene(root1));
        stage.show();
    } catch (IOException e) {
        e.printStackTrace();
    }
}}

And each of the FXML files controllers calls the constructor of ScreenFXController in its own constructor, like this:

public IndexMenu(){
    ScreenFXController screenFXController = new ScreenFXController("/com/bitdhelper/pages/IndexMenu.fxml", "Index", false, false, false);
}

Then to test this I changed the start method in the Main class to:

@Override
public void start(Stage stage) throws Exception {
    IndexMenu indexMenu = new IndexMenu();
}

And it worked! ...Until it didn't. I didn't made any change in the code, only in the FXML files via SceneBuilder. Here's the IndexMenu.fxml btw:

<?import javafx.geometry.Insets?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.control.ScrollPane?>
<?import javafx.scene.control.TextArea?>
<?import javafx.scene.control.TextField?>
<?import javafx.scene.layout.AnchorPane?>

<AnchorPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="407.0" prefWidth="500.0" xmlns="http://javafx.com/javafx/18" xmlns:fx="http://javafx.com/fxml/1" fx:controller="com.bitdhelper.pages.IndexMenu">
   <children>
      <Label contentDisplay="RIGHT" graphicTextGap="10.0" layoutX="21.0" layoutY="47.0" prefHeight="25.0" prefWidth="225.0" text="Name">
         <graphic>
            <TextField fx:id="tx_name" prefHeight="25.0" prefWidth="181.0" />
         </graphic>
      </Label>
      <Label contentDisplay="RIGHT" graphicTextGap="10.0" layoutX="261.0" layoutY="47.0" prefHeight="25.0" prefWidth="225.0" text="Category">
         <graphic>
            <TextField fx:id="tx_category" prefHeight="25.0" prefWidth="164.0" />
         </graphic>
      </Label>
      <Label contentDisplay="BOTTOM" layoutX="21.0" layoutY="79.0" prefHeight="221.0" prefWidth="461.0" text="Description">
         <graphic>
            <ScrollPane hbarPolicy="NEVER">
               <content>
                  <TextArea fx:id="tx_description" prefHeight="200.0" prefWidth="460.0" wrapText="true" />
               </content>
            </ScrollPane>
         </graphic>
         <opaqueInsets>
            <Insets />
         </opaqueInsets>
      </Label>
      <Label contentDisplay="BOTTOM" layoutX="21.0" layoutY="300.0" prefHeight="87.0" prefWidth="461.0" text="Other">
         <graphic>
            <ScrollPane hbarPolicy="NEVER">
               <content>
                  <TextArea fx:id="tx_other" prefHeight="55.0" prefWidth="460.0" wrapText="true" />
               </content>
            </ScrollPane>
         </graphic>
      </Label>
   </children>
</AnchorPane>

Everything else is the same, nothing's changed. But I started getting this error (Just showing part of it here, because it is very very long [11875 lines in total] - here's the full error JavaFX Error.txt):

at javafx.fxml@18.0.1/javafx.fxml.FXMLLoader.constructLoadException(FXMLLoader.java:2707)
    at javafx.fxml@18.0.1/javafx.fxml.FXMLLoader$ValueElement.processAttribute(FXMLLoader.java:944)
    at javafx.fxml@18.0.1/javafx.fxml.FXMLLoader$InstanceDeclarationElement.processAttribute(FXMLLoader.java:981)
    at javafx.fxml@18.0.1/javafx.fxml.FXMLLoader$Element.processStartElement(FXMLLoader.java:230)
    at javafx.fxml@18.0.1/javafx.fxml.FXMLLoader$ValueElement.processStartElement(FXMLLoader.java:755)
    at javafx.fxml@18.0.1/javafx.fxml.FXMLLoader.processStartElement(FXMLLoader.java:2808)
    at javafx.fxml@18.0.1/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2634)
    at javafx.fxml@18.0.1/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2548)
    at javafx.fxml@18.0.1/javafx.fxml.FXMLLoader.load(FXMLLoader.java:2516)
    at Main.start(Main.java:26)
    at javafx.graphics@18.0.1/com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$9(LauncherImpl.java:847)
    at javafx.graphics@18.0.1/com.sun.javafx.application.PlatformImpl.lambda$runAndWait$12(PlatformImpl.java:484)
    at javafx.graphics@18.0.1/com.sun.javafx.application.PlatformImpl.lambda$runLater$10(PlatformImpl.java:457)
    at java.base/java.security.AccessController.doPrivileged(Native Method)
    at javafx.graphics@18.0.1/com.sun.javafx.application.PlatformImpl.lambda$runLater$11(PlatformImpl.java:456)
    at javafx.graphics@18.0.1/com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:96)
    at javafx.graphics@18.0.1/com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
    at javafx.graphics@18.0.1/com.sun.glass.ui.win.WinApplication.lambda$runLoop$3(WinApplication.java:184)
    at java.base/java.lang.Thread.run(Thread.java:834)
Caused by: java.lang.reflect.InvocationTargetException
    at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
    at java.base/jdk.internal.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
    at java.base/java.lang.reflect.Constructor.newInstance(Constructor.java:490)
    at javafx.fxml@18.0.1/javafx.fxml.FXMLLoader$ValueElement.processAttribute(FXMLLoader.java:939)
    ... 17 more
Caused by: java.lang.NoClassDefFoundError: Could not initialize class javafx.scene.control.Button
    at jdk.internal.reflect.GeneratedConstructorAccessor4.newInstance(Unknown Source)
    at java.base/jdk.internal.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
    at java.base/java.lang.reflect.Constructor.newInstance(Constructor.java:490)
    at javafx.fxml@18.0.1/javafx.fxml.FXMLLoader$InstanceDeclarationElement.constructValue(FXMLLoader.java:1020)
    at javafx.fxml@18.0.1/javafx.fxml.FXMLLoader$ValueElement.processStartElement(FXMLLoader.java:757)
    at javafx.fxml@18.0.1/javafx.fxml.FXMLLoader.processStartElement(FXMLLoader.java:2808)
    at javafx.fxml@18.0.1/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2634)
    at javafx.fxml@18.0.1/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2548)
    at javafx.fxml@18.0.1/javafx.fxml.FXMLLoader.load(FXMLLoader.java:2516)
    at com.bitdhelper.utils.ScreenFXController.createScreenFX(ScreenFXController.java:21)
    at com.bitdhelper.utils.ScreenFXController.<init>(ScreenFXController.java:15)
    at com.bitdhelper.pages.MainMenu.<init>(MainMenu.java:16)
    ... 22 more

After searching the web, trying a lot of solutions and getting nowhere I went back to basics and tried the first step again, creating a new Window with the following code in the start method:

@Override
public void start(Stage stage) throws Exception {
    try {
        URL fxmlLocation = this.getClass().getResource("/com/bitdhelper/pages/MainMenu.fxml");
        FXMLLoader fxmlLoader = new FXMLLoader(fxmlLocation);
        Parent root1 = fxmlLoader.load();

        stage.setScene(new Scene(root1));
        stage.show();
    } catch (Exception e) {
        e.printStackTrace();
    }
}}

And now this code is also giving me the same error!

I've searched online, tried re-installing javafx, tried making new project, tried javafx via SDK and Maven,... nothing resolved this.

Does anybody know what's going on here?

0 Answers
Related