Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Insert an image into Java program

 
Roger Fed
Ranch Hand
Posts: 82
Java Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I know that when i want to create an image icon i can write this for example

but if i want to run this program in another computer the image will not appear as the path is incorrect
so, I want to save this image in my program so as to run and appear in any computer regardless the path name of the image.
How can i accomplish this?
thanks in advance.........
 
Rob Spoor
Sheriff
Pie
Posts: 20606
60
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Check out how resources work, then use getClass().getResource(). This returns a URL that you can pass directly to the ImageIcon constructor.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic