1

I have problem with Image Icons. I know that this have been asked before, but I can't figure out how to fix this, becouse I think my problem is different. When I launch app with eclipse, all works. But when I make it runnable jar, it won't show images.

Some code of my ImagesHolder Class :

package Clicker;

import javax.swing.ImageIcon;

public class ImagesHolder {

       final public ImageIcon AccessoriesIcon = new ImageIcon("Images/Part_Accessories.png");
       final public ImageIcon BodyIcon = new ImageIcon("Images/Part_Body.png");
       final public ImageIcon BrakesIcon = new ImageIcon("Images/Part_Brakes.png");
       final public ImageIcon CoolingIcon = new ImageIcon("Images/Part_Cooling.png");
       final public ImageIcon ElectronicsIcon = new ImageIcon("Images/Part_Electronics.png");
       final public ImageIcon EngineIcon = new ImageIcon("Images/Part_Engine.png");
       final public ImageIcon ExaustIcon = new ImageIcon("Images/Part_Exaust.png");
       final public ImageIcon FuelIcon = new ImageIcon("Images/Part_Fuel.png");
       final public ImageIcon InteriorIcon = new ImageIcon("Images/Part_Interior.png");
       final public ImageIcon SteeringIcon = new ImageIcon("Images/Part_Steering.png");
       final public ImageIcon SuspensionIcon = new ImageIcon("Images/Part_Suspension.png");
       final public ImageIcon TransmissionIcon = new ImageIcon("Images/Part_Transmission.png");
       final public ImageIcon TiresIcon = new ImageIcon("Images/Part_Tires.png");

And If I make Images as URL, i can't reset image icons, like here (I have Labels and I want to change label icon)

Labels example :

   public JLabel AccessoriesLVL1Label = new JLabel(ImagesHolder.LockedIcon);
   AccessoriesLVL1Label.setHorizontalTextPosition(JLabel.CENTER);
        AccessoriesLVL1Label.setVerticalTextPosition(JLabel.BOTTOM);
        AccessoriesLVL1Label.setText("<html>Accessories LVL 1<br>" + "Count: " + Part.parts[1]);

And change :

if(CarMain.main[5] >=1){
            jbtnSellAccessoriesLv1.setEnabled(true);      
            Labels.AccessoriesLVL1Label.setIcon(ImagesHolder.AccessoriesIcon);         
        }

Edited: If I this :

final public ImageIcon MoneyIcon = new ImageIcon("Images/Money.png"); 

Make as :

URL MoneyIcon = ImagesHolder.class.getResource("/Money.png");

I get error in this line:

Labels.MoneyLabel.setIcon(ImagesHolder.MoneyIcon);

Error :

The method setIcon(Icon) in the type JLabel is not applicable for the arguments (URL)
MsCrelix
  • 55
  • 8
  • First thing is first, have you checked that they are inside the jar? Just in case... – this.user3272243 Mar 21 '16 at 10:33
  • @user3272243 Yes, I opened jar file as zip, and there was all Images. But they were in main, not in Images Folder. I tried to change Image Location (In Code, to work, if Images are in main folder), but still didn't showed any Image – MsCrelix Mar 21 '16 at 10:35
  • If you move them inside the jar as if it was a zip to the "correct location" do they work? Try also to use an absolute route an place them in that location see if the jar works that way to discard if its trully the jar not working or if its a problem on your side. – this.user3272243 Mar 21 '16 at 10:37
  • @user3272243 Sorry, I don't have very good english, and i didn't understood you. Maybe you can please explain, somehow different? Here is image, where images are in jar file : https://imgur.com/LvBcvYN – MsCrelix Mar 21 '16 at 10:40
  • [Check this answer](http://stackoverflow.com/questions/30275143/java-io-filenotfoundexception-the-system-cannot-find-the-path-specified-cvs-fi/30275207#30275207). It'll help you. – ELITE Mar 21 '16 at 11:08

2 Answers2

3

There is an alternative for getting images from jar file:

Ideal way would be to have a resource directory under your project root and include it into the list of source code directories. This will result in all images there being copied into the JAR. If you make a sub directory there, resources/image, then you'll end up with a JAR that has an image directory. You access those images through the classloader:

classloader.getResourceAsStream("/image/image1.jpg");

or,

classloader.getResource("/image/image2.jpg");

Refer this link for in detail example.

Community
  • 1
  • 1
Dark Knight
  • 8,218
  • 4
  • 39
  • 58
  • Please, check out my Edit. – MsCrelix Mar 21 '16 at 10:48
  • *"There is an alternative for getting images from jar file: .. `getResource`"* That makes it sound like there is another way than accessing the resource by URL. Actually I can think of one 'alternative' but it is too horrendous to detail. No. `getResource` (or `getResourceAsStream`) is simply **the way** to get resources from Jar files. Anything else is an 'alternative'. – Andrew Thompson Mar 21 '16 at 10:49
  • @MsCrelix Kindly make sure you have images under resource directory. – Dark Knight Mar 21 '16 at 10:51
  • *"Please, check out my Edit."* You really need to consult the JavaDocs for the classes and methods being used. Programming without the documentation is like trying to fight with one hand tied behind your back. *N.B. One of the **constructors** for an `ImageIcon` will accept an `URL`.* – Andrew Thompson Mar 21 '16 at 10:51
2

Well, to sum up the previously given answers: I guess a potential solution would be to use classloader as Dark Knight proposed. Furthermore you just need to create a new ImageIcon before setting it.

URL MoneyIcon = ImagesHolder.class.getResource("/Money.png");
Labels.MoneyLabel.setIcon(new ImagesIcon(ImagesHolder.MoneyIcon));

Because if you look through your code again, I guess it somewhat makes sense it does not work. I mean you want to set an ImageIcon, yet the last thing you have is an URL object. That this must throw an error should make sense. Yet as Andrew Thompson already said: You can construct an ImageIcon from that URL as displayed/explained above.

And since you also explained that you had issues with the directories (getting the structure from source to compiled) I want to add some links for further reading: If you just use 'plain' Java (no build tools) maybe this link can help: Copying data files upon build in Java/Eclipse. It is mainly written for Eclipse, but I guess most other IDEs should provide similar options.

From my current projects I could also recommend to use a tool like Maven. For smaller projects it might be a little over the top, but it takes of a lot of work (e.g. library handling, handling of resources like images etc.). These are just some parts that might be useful for you. If this is of interest for you I guess you will find lots of useful resources on the web since Maven (or other tools like Ant and Gradle) are quite commonly used.

Community
  • 1
  • 1
Marco N.
  • 185
  • 2
  • 8