Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Define Image folder in program.  RSS feed

 
Edmund Petrof
Greenhorn
Posts: 9
Netbeans IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello all, i am still new in java. I have make small portable application for my needs.

So as usual i got small problem about image location and such.

The problem is My Application is Portable whit Crud Database in it. Have one Jlabel who display Image ( image path is saved in Table column)

If I move Application from my HDD to someone else i Image path is not the same.

I will write small example to explain better.

In table column i save this path "c:\MyImage\IMG\Image1.png"

so code for jlabel to laod would be.
jLabeltest.setIcon(new ImageIcon( "c:\MyImage\IMG\Image1.png")

Is there a way to define folder to where to read and save same path in the table. I would make a folder in program and there put(arrange images in it, update etc )

So that i can save path in table like "IMG\1st.png" not whole path. I cant put in Jar that Image because i need to update daily.

I hope i have explain good what i mean. I dont look for Code just poit me to some topic or example i will try to swim my way to make what i need. Ty for you time
 
Danny Wreczycki
Greenhorn
Posts: 23
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes it is. Use getClass().getResource("/folderWhereImageIs/nameOfImage.jpg"); I belive it will start looking for an image/file from folder where application entry point is so from where is class with contains main method.
 
Edmund Petrof
Greenhorn
Posts: 9
Netbeans IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yea i was reading some article about it. Basically what i need to edit is Jfilechooser when selcet picture just to trim Path from aphsolute to "/folderWhereImageIs/NameOfImage.jpg/png"



Or just to trim path to Image Name and like that save to Database table?
 
Tony Docherty
Bartender
Posts: 3268
82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you want to have your image folder locatable on different installations you could:

1. Place the image folder relative to the user.home system property setting
2. Place the image folder as a sub folder of the root folder of your application.
3. Place it relative to the jar file containing for application and use the classloader to determine the location of the jar file on the file system. See this article (with code) http://illegalargumentexception.blogspot.co.uk/2008/04/java-finding-application-directory.html.
 
Edmund Petrof
Greenhorn
Posts: 9
Netbeans IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am think to set image folder in the application. So i am only confused how to trim that path from Jfilechoser who insert into Jtable column path of image. Then Jlabel read path and display it.

URL url= getClass().getResource("/folderWhereImageIs/nameOfImage.jpg") work so well. I have option to save path and then manualy remove C:\\somefoleder\ApplicationFolder\

I guess i got puzled only by that triming the path from jfilechooser.

I will read that article for now. Maybe i will workout something from it. Ty on your post.
 
Tony Docherty
Bartender
Posts: 3268
82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
An easy way to get the folder path from your file name string is to create a File object from the file name and then use getParent() method.
Note: if you create the File object with just a file name and no parent directories you will need to use the getAbsoluteFile() first to get a File object which includes a path.
If you want just the file name then call the getName() method.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!