-1

I have an image file which I want shown as a JLabel in a JFrame for a program that will be on several computers running the same code. The image would not be on the computers already but would somehow be stored as a program file. The computers would all be windows. To insert the image a file path has to be given, but I'm not sure what this will be considering the computers are all different. I have done this before on a different language by having the program find the program's directory and doing some string manipulation, but I have not been able to do this yet on java and would like to know if there is a better way.

Here is the statement that the path has to go into:

lblTitleBG.setIcon(new ImageIcon(file path goes here));

lblTitleBG is a JLabel.

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
TeeMee123
  • 151
  • 1
  • 1
  • 10
  • Could you clarify your question?Is this image distributed as part of your application? Or does it simply already exists on those machines? Do you need to be concerned with cross-platform (different OSs)? For reference file paths can be different for different OS types such as Windows (c:\filename.txt) or Linux "/home/user/filename.txt". – Joshua Briefman Aug 25 '16 at 00:00
  • I edited the question – TeeMee123 Aug 25 '16 at 00:06

1 Answers1

-2

First you create a source folder inside your project using eclipse or your preferred IDE. That will create a physical folder in your project folder that you can navigate in and move your files.

Let's say you have Project1 where you create Files_Folder. Now you navigate to the Project1 folder with window explorer and paste the files you need inside the Files_Folder and in the bin folder too. After this, just go back to eclipse and refresh/clean project. Your new moved files will appear in the Files_Folder in eclipse explorer too.

Just use lblTitleBG.setIcon(new ImageIcon("Files_Folder/image.png")); after following all the steps and you will have portable code.

Try to pay attention to steps and everything will work good. And finally, when you export your jar, don't forget to keep the Files_Folder in the same location to the jar, in order to find and use the needed files.

Andrei Mesh
  • 256
  • 2
  • 20
  • The requestor has not provided enough information for this answer to be applicable without making unverified assumptions about their actions. – Joshua Briefman Aug 25 '16 at 06:07
  • Thanks, this solved my problem! – TeeMee123 Aug 25 '16 at 10:50
  • From his edited question, things appear pretty clear. He just needed a way to keep his external files tied to his project without linking them dependent to platform like C:\Program Files\... just so he could transfer his program to other machines without having to recreate the path in order to work. I provided the easiest way to do that and I think even if I talked only about the eclipse way to do this, I was pretty clear, as you can see it solved his problem. First understand, then vote. @Joshua Briefman – Andrei Mesh Aug 25 '16 at 13:28
  • Ah I see the edited question explicitly says those files are not stored within the Jar, handling that case wasn't covered or even mentioned in your answer which is where my comment came from. I can adjust my vote but I can only do so if the answer is edited as 11 hours have passed. – Joshua Briefman Aug 25 '16 at 17:24