Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JavaFX Image not showing in stage

I've tried several times and several ways but I can't make my image show on stage as I want. I think it might has to do with the path where java looks for resources, but i'm not sure, since I'm just starting using visual libraries (JavaFX in this case). Here's my directory structure:

MyProject
 |_assets
 |  |_img
 |     |_myImage.jpg
 |
 |_some
 |_other
 |_folders
 |
 |_src
    |_ve
       |_org
          |_project
             |_MyProject.java
             |_StratPage.fxml
             |_StartPageController.java

I need to retreive myImage.jpg to be rendered, and I've tried the following:

1) Pure fxml approach:

<ImageView
     id="logo" 
     fx:id="logo"
     fitHeight="99.0" 
     fitWidth="99.0" 
     layoutX="14.0" 
     layoutY="18.0" 
     pickOnBounds="true" 
     preserveRatio="true">
         <image>
            <Image url="@../../../../assets/img/myImage.jpg" />
         </image>
</ImageView>

2) Using both fxml and java. Declaring the ImageView element with fx:id="logo", and injecting the image from StartPageController.java like this:

public class StartPageController implements Initializable {

    @FXML
    private ImageView logo;


    @Override
    public void initialize(URL url, ResourceBundle rb) {
        this.logo = new ImageView("file:../../../../assets/img/myImage.jpg");
    }    

}

Neither way produce any exception, i just does not show the image. I have no idea what to do. I would really appreciate your help.

UPDATES:

First

I tried giving up on having the proposed directory structure, and placed the image file in the same folder of StartPageController.java. By doing

logo = new ImageView(new Image(getClass().getResourceAsStream("myImage.jpg")))

I'm not getting any exception, but the image is not rendering, which suggest me it's not about finding the resource, but about rendering the image. Could it be the lack of any library? I'm on a Windows 8 environment, using Netbeans 8.0. Thanks again for your answers.

Second

I just deactivated packaging and distributing the app in the project properties in Netbeans. Now the images are rendering correctly, but I don't consider the issue solved, since when I need to distribute the software it will re emerge. Please, help is still needed! :)

like image 430
Throoze Avatar asked Jun 17 '14 09:06

Throoze


3 Answers

I checked the code in my own project and it works with snippet below. I've adjusted it to your example

 this.logo = new ImageView(new Image(getClass().getResourceAsStream("/assets/img/myImage.jpg")));
like image 60
Perneel Avatar answered Oct 22 '22 02:10

Perneel


I had the same error as you. So to correct it placed my image in resources folder in the same level of "src" of my project. This is my code, and it works.

public void showImage() {
    try {
        Image image = new Image("resources/img/akonolingaMap.jpg");
        imageView.setImage(image);
        imageView.setCache(true);
    } catch (Exception e) {
        printStackTrace();
    }
}
like image 24
Anatole ABE Avatar answered Oct 22 '22 01:10

Anatole ABE


If you use Intellij as your main editor, then you could mark your folder as "Resources" or just open "Project Structure" -> "Modules" -> then mark your image folder as "Resources"

Hope that will help you.

like image 28
PiBits Avatar answered Oct 22 '22 01:10

PiBits