Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-8128843

FXMLLoader fails to load ImageViews when using non-static FXMLLoader.load(InputStream) method

    XMLWordPrintable

    Details

    • Type: Bug
    • Status: Resolved
    • Priority: P4
    • Resolution: Not an Issue
    • Affects Version/s: fx2.0
    • Fix Version/s: None
    • Component/s: javafx
    • Labels:
      None
    • Subcomponent:

      Description

      When using the static FXMLLoader.load(URL) method the following FXML loads correctly:

        <?fxml version="1.0" encoding="utf-8"?>
        <?import javafx.scene.image.*?>
        <ImageView>
            <image
              <Image url="@images/my_image.png"/>
            </image>
        </ImageView>

      However when using the non-static FXMLLoader.load(InputStream) class, this fails with the following error:

        Caused by: java.lang.InstantiationException: javafx.scene.image.Image
      at java.lang.Class.newInstance0(Class.java:357)
      at java.lang.Class.newInstance(Class.java:325)
      at javafx.fxml.FXMLLoader$InstanceDeclarationElement.constructValue(FXMLLoader.java:607)

      I have tried using relative and absolute paths (i.e. with and without '/' on the front) and also tried putting the image in the same package as both the loading class and the FXML file. All have the same result.

      Minimal setup to reproduce (using the FXML defined above):

      public class TestApp extends Application
      {
          public static void main(String[] args)
          {
              Application.launch(TestApp.class, args);
          }

          public void start(Stage stage) throws Exception
          {
              // Node node = (Node) FXMLLoader.load(getClass().getResource("/test.fxml")); this one works

              FXMLLoader loader = new FXMLLoader();
              Node node = (Node) loader.load(getClass().getResourceAsStream("/test.fxml")); // this one fails

              VBox box = new VBox();
              box.getChildren().add(node);
              Scene scene = new Scene(box);
              scene.getStylesheets().add("stylesheet.css");
              stage.setScene(scene);
              stage.setWidth(1024);
              stage.setHeight(768);
              stage.show();
          }
      }

      This bug was spotted by Robert. Forum topic here: https://forums.oracle.com/forums/thread.jspa?threadID=2279877&tstart=0

        Attachments

          Activity

            People

            Assignee:
            gkbrown Greg Brown (Inactive)
            Reporter:
            dzwolenskjfx Daniel Zwolenski (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Dates

              Created:
              Updated:
              Resolved:
              Imported: