1

I have an annoying problem with the relative file-path on eclipse. I've seen already many posts about how to fix it but nothing works for me.

My directory structure is the following:

enter image description here

Inside of the C.java I I want to indicate the relative path to the NotSet_16x16.png

 ip_address_image_label.setIcon (new ImageIcon ("<relativePath>\\NotSet_16x16.png"));
Alex
  • 1,054
  • 6
  • 25
  • 42

4 Answers4

3

Don't keep resources with the code. Create a separate 'resources' folder (a source folder along with the src you already have), an images folder in it, and keep those PNGs there. Do the same for the rest of your resources (I see you have a csv and a txt file).

When done with that, you will see that you shouldn't access your resources having in mind the location of the class where you need them, but only having in mind that they are resources and all of them are at the same place, i.e. get them with an absolute path like /images/NotSet_16x16.png. So in your class you could do something like:

new ImageIcon(this.getClass().getResource("/images/NotSet_16x16.png"))
alterfox
  • 1,675
  • 3
  • 22
  • 37
  • System.out.println(this.getClass().getResource("/images/NotSet_16x16.png")); this statement returned NULL – Alex Oct 16 '13 at 00:04
  • I just created a new resource folder for the image files. I updated the picture with the new directory structure in the question. – Alex Oct 16 '13 at 00:09
  • 1
    Try System.out.println(this.getClass().getResource("/NotSet_16x16.png")); since you haven't created a folder inside that source folder (do not reference a source folder, only folders in it). – alterfox Oct 16 '13 at 00:13
  • I still recommend that your new source folder be named 'resources' and to create separate folders in it. Otherwise you will be tempted to create a new _source_ folder for documents, etc. Cheers! – alterfox Oct 16 '13 at 08:17
  • `this.class` is invalid syntax, you should use `this.getClass()` instead. – WonderCsabo Jan 07 '15 at 17:11
1

Generally files within the code are used as resources. After compilation they end up with the class files, and finally in a .jar file.

So read the data into a byte[] from the C class as a resource and use the ImageIcon(byte[]) constructor to create the image.


Alternatively you may use an intermediate image which could be read by using an URL to a resource to create the icon. This is probably more neat as you don't have to bother yourself with reading an InputStream into a byte[] buffer.


Note that you can simply use the name of the file directly if you use either of these methods (as the class file and the image are in the same package). You don't need to specify a path.

Community
  • 1
  • 1
Maarten Bodewes
  • 90,524
  • 13
  • 150
  • 263
1

Try this one:

String filePath = ".\\images\\NotSet_16x16.png";

where «.\» is a root for Eclipse project, «images» is a folder with user's files inside of Eclipse project. Since we are talking about Windows OS, we have to use «\» and not «/», like in Linux, but «\» is the reserved symbol, so we have to type «\» to get desired result.

Mike
  • 14,010
  • 29
  • 101
  • 161
1

I tried all these answers for my problem, and none of them worked. I asked a friend and he answered my problem perfectly. Create a source folder called Images (ie if you're using eclipse, right-click your project -> new ->sourceFolder). Call it whatever you want, i called my Images. Put some images in it.

Now i had JLabels where i gave them ImageIcons. Look at the following code.

    ImageIcon BPawn;
    ImageIcon WPawn;
    JLabel Label = new JLabel[8][8] //2D array of labels that gives each spot a position.
    public void setPieces(){
        //set up pawns in their respective positions.
        BPawn = new ImageIcon("Images/BPawn.png", "BPawn");
        WPawn = new ImageIcon("Images/WPawn.png", "WPawn");
    for(int i=0;i<Label[0].length;i++){
        Label[1][i].setIcon(BPawn);
        Label[6][i].setIcon(WPawn);
    }//end for

    }//end setPieces.

There is a lot more in setPieces() method, but this glimpse is how you would reference the images in your source folder when you create an executable jar and want the images to show up.

A-a-ron
  • 68
  • 7