So how do I change the icon of a folder in Java (Windows system) is there a class or something cause I have searched and I can't find anything...
Asked
Active
Viewed 3,434 times
2
-
Do you mean the icons shown in the JFileChooser? Or the actual icon used for that folder by Windows, regardless of whether Java is involved? – DNA Feb 17 '12 at 15:05
-
1You need to use the windows API. I don't know of any Java wrappers for that part of the API so you would have to make a small JNI program that calls the respective C function. I assume that you mean the icons inside the Windows explorer? Or if you mean individual folders then just use this: http://stackoverflow.com/questions/1115012/custom-windows-folder-icon – PeterT Feb 17 '12 at 15:06
-
yes I mean my actual folders that I have so I need to tinker the hidden ini file in each folder to make it point to the icon that I want right? – Aki K Feb 17 '12 at 15:12
-
@TheByt3 not every folder has that file. So if it's not there then you need to create it first. You can just write to it like a textfile but it would be better to use some kind of INI-Class or the Windows-API if you can tolerate JNI but that's probably overkill. – PeterT Feb 17 '12 at 15:19
-
yea I'm kinda new to programming I just know how to code in java and c so I guess what you are telling me is that it's not that good to write into it like a text file or make it but better use an INI-Class (guess I can find that type of class in Java and C++ but I suppose it would be better in C++) or use the Windows-API which I have no idea what it is :P – Aki K Feb 17 '12 at 15:26
2 Answers
1
Based on the comments, the folder icon that you are talking about is specified in a hidden "ini" file in the folder itself.
You could create / modify the file by reading it as text, etcera, but it is simpler to use an existing 3rd-party Java library. I've had success using the open-source ini4j Java library.

Stephen C
- 698,415
- 94
- 811
- 1,216
0
I did that using ini4J and It is working with me under only one condition : Folder path shouldn't have any space.
The Code:
// Create destop.ini file
writer = new BufferedWriter("your folder path without any spaces");
writer.write("");
writer.close();
// Set file attributes hidden and system and set folder as system folder and not hidden
Wini ini = new Wini("your folder path without any spaces");
String field = "icon Path" + ",0";
ini.put(".ShellClassInfo", "IconResource", field);
ini.store();
Process processCreateFile = Runtime.getRuntime().exec("attrib +h +s " + "desktop.ini file path");
Process processCreateFolder = Runtime.getRuntime().exec("attrib -h +s " + "your folder path without any spaces");

KapoUS
- 19
- 4