UnreachableCode
UnreachableCode

Reputation: 1791

Unable to import image resources into our GUI

I'm working on a project with some friends over Github for a University project. I've only just taken my friends code off the repository for the first time. For some reason, all references to images in the code don't seem to allow compilation due to a directory problem I think. I'm using Eclipse, my friend's using Netbeans(don't know if that affects it or not?). Anyway, all of the images referenced are either in a folder known as runone, on the same level as the Eclipse src, or within the package 'runone' within src. I don't know which.

Here's an example of some of the references:

jLabel2.setIcon(new javax.swing.ImageIcon(getClass().getResource("/runone/OSTeeeennny.jpg")))

jLabel53.setIcon(new javax.swing.ImageIcon(getClass().getResource("/runone/clown_fishTIny.jpg")));

I guess what I'm wanting to know is, how can I make these resources work correctly, on any machine that we code this program on?

Hope that all made sense!

Upvotes: 0

Views: 244

Answers (1)

ughzan
ughzan

Reputation: 1578

For the examples you have given your file structure might look like this

src/com/yourpackage/YourClass.java
src/runone/OSTeeeennny.jpg
src/runone/clown_fishTIny.jpg

For a more best practice way of organizing your resources you could do this

src/com/yourpackage/YourClass.java
src/com/yourpackage/resources/OSTeeeennny.jpg
src/com/yourpackage/resources/clown_fishTIny.jpg

and then use the following calls to load them

getClass().getResource("resources/OSTeeeennny.jpg")
getClass().getResource("resources/clown_fishTIny.jpg")

Upvotes: 2

Related Questions