Monday, June 4, 2012

JavaFX 2: How to Load Image

This is JavaFX tutorial about how to load a image in your JavaFX 2 application. This can be done easily with ImageView. The ImageView is a Node used for painting images loaded with Image class. So as you can se we will first load image with Image class and then display it with ImageView. Also I will here demonstrate how to load image from local disk, and how to load image from Internet. First example is how to load an image from disk, then I'll show how to modify it, to load image from Internet.
-->
Load Image with ImageView in JavaFX 2

Okay, what if you want to load a image from some Internet location? Well, that can be also easily done, just modify this line of code. The result will be same.

-->

9 comments:

  1. not working for me, maybee cuz of fx 2.1 sdk, after 1 hour searching and reading oracle docs, found some workaround.

    I replaced Line 23 and 24 with

    ImageView imgView = new ImageView(new Image(LoadImage.class.getResourceAsStream("javafx.jpg")));

    ReplyDelete
  2. here better solution, only replace Line 23 with this code:

    Image img = newImage(LoadImage.class.getResourceAsStream("javafx.jpg"));

    ReplyDelete
  3. Maxim, thanks for all the feedback.

    I'm assuming that's similar problem like with setting stylesheet problem, that we discussed recently.

    ReplyDelete
  4. maked some tests and finally got it.i think it's simple pathfinding issue, if i place javafx.jpg straight in the project folder, Image img = new Image("javafx.jpg"); works :), if i put javafx.jpg in the src folder doesn't, so root is allways the project folder and if i move javafx.jpg to src folder so a path need some changes Image img = new Image("src/javafx.jpg");

    Now the other one Image img = new Image(getClass().getResourceAsStream("javafx.jpg")); for this function is the src folder allways the root and not the project folder =_=

    so eclipse and sdk are fine, and for the css files i think its the same just need to make some test and look up wich folder is the relativ root to start :) so u can use the short version

    very funny css path is scene.getStylesheets().add("login.css"); takes the src folder as root and not project folder,

    Conclusion:
    // root is the main project folder
    Image img = new Image("javafx.jpg");

    //root is the src folder
    scene.getStylesheets().add("login.css");

    ReplyDelete
  5. Amazing work here, Maxim. :)

    Thank you a lot, I will consider, test and use these your tips here.

    If you find something more useful please feel free to comment, and also share these JavaFX tutorials with your friends.

    Great, effort. :)

    ReplyDelete
  6. can i upload image from c:/ drive at run time

    ReplyDelete
  7. This comment has been removed by the author.

    ReplyDelete
  8. the trick :

    new ImageView(new Image(getClass().getResourceAsStream("Image.jpg")));
    worked for me.
    but only with the image in the source package.

    I tried to load from an external source as C:/User/Pictures/image.jpg
    and it doesn't work. what should I code? thanks

    ReplyDelete