1

I have a little problem with Swing (or maybe netBeans), I was making a testing JFrame, I changed its background by an image with this line of code:

this.setContentPane(new JLabel(new ImageIcon(ImageIO.read(new File("src/testdesign/BG.png")))));

And I added a JLabel with an Icon to the frame using netBeans. The result when compiling the project (In the IDE) was:

enter image description here

All is fine.. But when I generate a JAR file, the result is:

enter image description here

I'm not a Java pro, so I think that the way I changed the background isn't the proper way, or maybe it's another thing.

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
user3426711
  • 344
  • 4
  • 16
  • possible duplicate of [Trying to load icon from jar file](http://stackoverflow.com/questions/1133066/trying-to-load-icon-from-jar-file) – alex2410 Feb 13 '15 at 12:09

1 Answers1

2

You use incorrect way to load background image. In your code image location depends on project directory src. You should load image from classpath but file system. You Class.getResource method to fix it e.g.:

this.setContentPane(new JLabel(new ImageIcon(getClass().getResource(pathToImage))));
Sergii Lagutin
  • 10,561
  • 1
  • 34
  • 43
  • Thanks for your answer Sergey, but I get an error : non static method cannot be re... from a static context... I put is in the constructor which is called from main (static), even if I put it in another methot all of them are called by main.. – user3426711 Feb 13 '15 at 12:01