简体   繁体   中英

Eclipse relative file-path to the image file

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:

在此处输入图片说明

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

 ip_address_image_label.setIcon (new ImageIcon ("<relativePath>\\NotSet_16x16.png"));

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, ie 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"))

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.

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM