AlmasB / FXGL

Java / JavaFX / Kotlin Game Library (Engine)
http://almasb.github.io/FXGL/
MIT License
4.45k stars 555 forks source link

Using loadUI to load fxml generated by SceneBuilder, the resource path @ symbol is null #1343

Closed wendyunfa closed 9 months ago

wendyunfa commented 9 months ago

Dear Almas: When I use loadUI to load the fxml generated by SceneBuilder drag and drop, if there is an Image tag in the fxml file, and the resource path that starts with @ will prompt javafx.fxml.LoadException: Unknown path: 21 代码:

            UI ui = FXGL.getAssetLoader().loadUI("fxml/CitySubScene.fxml", new CitySubScene());
            getGameScene().addUI(ui);

Snipaste_2024-02-14_23-39-26

AlmasB commented 9 months ago

Hi, thanks for this. Please can you include the image, fxml file and the minimal code that reproduces this issue so we can test and fix it as appropriate.

wendyunfa commented 9 months ago

Thank you for your reply. Simply add the following code to the initUI method that extends the overrides of the GameApplication class to reproduce

    protected void initUI() {
        UI ui = getAssetLoader().loadUI("fxml/CitySubScene.fxml", () -> {});
        getGameScene().addUI(ui);
    }

Resource path of fxml assets/ui/fxml/CitySubScene.fxml

<?xml version="1.0" encoding="UTF-8"?>

<?import javafx.scene.image.Image?>
<?import javafx.scene.image.ImageView?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.layout.Pane?>
<?import javafx.scene.layout.StackPane?>

<Pane xmlns="http://javafx.com/javafx/21" xmlns:fx="http://javafx.com/fxml/1">
   <StackPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="800.0" prefWidth="1280.0">
      <children>
          <AnchorPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="800.0" prefWidth="1280.0">
            <children>
               <ImageView fitHeight="416.0" fitWidth="298.0" layoutX="61.0" layoutY="63.0">
                  <image>
                     <Image url="@CityDataWindow.png" />
                  </image>
               </ImageView>
            </children>
         </AnchorPane>
      </children>
   </StackPane>
</Pane>

Resource path of IMageassets/ui/fxml/CityDataWindow.png CityDataWindow

AlmasB commented 9 months ago

@wendyunfa thanks for the clear examples, this should now be fixed by the attached commit.

Let me know if it works.

To get the latest build, you can use this:

<dependencies>
    <dependency>
        <groupId>com.github.almasb</groupId>
        <artifactId>fxgl</artifactId>
        <version>21+dev-SNAPSHOT</version>
    </dependency>
</dependencies>

<repositories>
    <repository>
        <id>oss.sonatype.org-snapshot</id>
        <url>https://oss.sonatype.org/content/repositories/snapshots</url>
    </repository>
</repositories>
wendyunfa commented 9 months ago

Thank you very much. After testing, this problem has been fixed.