How to get the icon of an application on mac? Java - java

I am trying to get the image of a file on mac, But i cant find any answers
There is some code that works on windows.
String s = "c:/windows/regedit.exe";
File file = new File(s);
sun.awt.shell.ShellFolder sf =
sun.awt.shell.ShellFolder.getShellFolder(file);
Icon icon = new ImageIcon(sf.getIcon(true));

There's no clear-cut answer for this. You'll have to do some investigation yourself. If you have an Application in Finder, right-click and choose 'Show package contents'. In the Contents folder, search for the Info.plist file. You'll find an entry containing CFBundleIconFile like this (I took TextEdit as an example):
<key>CFBundleIconFile</key>
<string>Edit.icns</string>
Go inside the Resources folder, and there you will find the .icns file. You can find information on how to do that in this question.

Related

create folder with app icon instead of default folder icon

I want to add my app Icon to created folder instead of default folder icon.
I know i can create folder with following code.
String extr = Environment.getExternalStorageDirectory().toString();
File mFolder = new File(extr + "/My Application/Downloads");
if (!mFolder.exists()) {
mFolder.mkdirs();
}
By doing this, i'm getting following result.
but what i want is..
I have searched a lot, but no luck. can anyone help me out?
Thank you.
Directories do not have icons, and so whatever app or tool your screenshot is from is adding that. You would have to ask the developers of that app or tool what algorithm they are using to determine the icon to show.
Customization of folder icon is OS dependent.
On Windows you will use Desktop.ini file to assign a custom icon or thumbnail image to a folder. To simplify working with Desktop.ini you can use ini4j. Basically, you will need to create Desktop.ini in the folder, that you want to customize and specify path to the icon there. (Here is similar question)
On Mac OS the process is different. There are ways to access icons (for example, FileView or FileSystemView, also Quaqua). However I haven't found a way to set folder icon programmatically with Java.
Note: I assumed, that you want to add icon to arbitrary folder and not your app laucher.
After creating new directory you have to declare resource directory in your gradle file so that android studio can recognize new resource directory.
More information please refer https://developer.android.com/studio/write/add-resources

Eclipse plugin and editor

How to open a file outside of the project programically in a new editor window ? I've been searching all over the internet and nothing seems to work.
I have eclipse 4.4.
I thought something so simple as opening a file will be easy but somehow it keeps me awake at night.
Please help.
I cannot comment as yet, so adding my question/suggestion as an answer.
What do you mean by your question? You mean something like a command to open a target file in some editor that is installed on your computer.
You could try using
Process process = Runtime.getRuntime ().exec ("<path to editor>/some_editor.exe");
If the program allows taking file to open as parameter, you could place that too in commmand above towards the end.
I hope this is what you are looking for.
To open an editor on a file which is not in the current workspace use:
String path = ... path to file
IFileStore fileStore = EFS.getLocalFileSystem().getStore(new Path(path));
IWorkbenchPage page = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage();
IDE.openInternalEditorOnFileStore(page, fileStore);
Note: some editors may not support files that are not in the workspace.

System.getProperty("user.dir") with Eclipse IDE

I've been blasting through the Java Swing tutorials on zetcode.com
I've made it all the way to Basic Swing Components II, the JTextPane component.
In this example, an HTML document is loaded into memory and placed into the textpane.
In order to find the file, zetcode.com uses:
String cd = System.getProperty("user.dir") + "/";
textPane.setPage("File:///" + cd + "test.html");
My IDE of choice is Eclipse Kepler.
I have written the code for this example's class and created the HTML document exactly as zetcode.com has shown on the page. I have placed the HTML file in the same source folder and package as the class which uses it.
But when I run the code, I hear a Windows system error sound and the JFrame pops up without any text inside the textpane.
EDIT 01:
I've named the package "com/zetcode/swingtutorial/basiccomponents/".
I've tried using getClass.getResource("/com/zetcode/swingtutorial/basiccomponents/test.html")
and I figure I must have typed this correctly because I do not get an IOException.
EDIT 02:
Here's another interesting thing:
In zetcode's system, they've used "File:///", which caused Windows to play an error sound.
But when I tried "File://", no error sound plays. D'you think that was just a typo on their part?
Either way, my html doc still isn't displayed on the pane. :S
Do you know what I could be doing wrong?
Many thanks for your help!
Try this:
textPane.setPage(YourClass.class.getResource("test.html"));
If its in package
textPane.setPage(YourClass.class.getResource("/packagename/test.html"));
System.getProperty("user.dir");
Gives you the root context location or the project location folder or you can say the current directory when JVM was started
Accordingly give the path or alternately
Try loading this way
String pathOfHtmlFile = Thread.currentThread().getContextClassLoader()
.getResource("yourHtmlFile").getPath();
textPane.setPage(pathOfHtmlFile);
provided the file is in the classpath.
System.getProperty("user.dir")
Returns the "working folder" from which the application was launched. In Eclipse I believe the working directory is the top level project folder (not in src) or it's in the folder that contains the compiled .class files. Try copying the file to those folders and see when it works.

Displaying images within a jar file

I know this question has been asked before, but I have gone over all the solutions I could find and cannot make any of them work for me. I have a program in Eclipse that I am trying to export to an executable jar file. It is the first time I have done this. When I execute the jar, my program images do not show up. So I did some research and found I need to load them as resources, but I cannot seem to make it work.
Here is the code I was using to load the images without the jar:
private void initComponents()
{
// create an enterprise icon and make it invisible
enterpriseIcon = new JLabel(new ImageIcon("res/enterprise1.png"));
enterpriseIcon.setVisible(false);
}
I have all my images in a folder named res in my root project directory, and I told Eclipse to put this folder in the build path.
When the images wouldn't show up when running the jar file, I tried the following:
enterpriseIcon = new JLabel(new ImageIcon(getClass().getResource("/res/enterprise1.png")));
and
enterpriseIcon = new JLabel(new ImageIcon(Toolkit.getDefaultToolkit().getImage(getClass().getResource("/res/enterprise.png"))));
However, when either of these is run from within Eclipse, I get a null pointer exception. (I tried a few other things as well, but the above were the only solutions I thought I understood.)
Any help getting this to work would be appreciated.
Use Class#getResource to get the URL of your image inside the jar file
enterpriseIcon = new JLabel(new ImageIcon(getClass().getResource("/res/enterprise1.png")));
Try this. This works well for me :)
private void initComponents() {
try {
BufferedImage icon = ImageIO.read(getClass().getResource("/enterprise1.png"));
frame.setIconImage(icon);
} catch (IOException e) {
e.printStackTrace();
}
}
I was also facing the same issue till yesterday. I have got a fix for this. Let me share it with you.
In your Eclipse, go to your Project name.
Right your project name.
New -> Source file/folder
Name this Source file/folder as images
Now on your local machine go to the Eclipse workspace where your project
is physically present.
Copy ALL your images into the newly created
'images' folder right there only.
Now go back to Eclipse.
Right click on your Project -> Refresh
Now, wherever you are using images in your .java files , go to those lines and prefix the image location with images/
Example : XXX xxx = XXXX (example.jpg)
Change it with .... XXX xxx = XXXX (images/example.jpg)
BOOM !!
The images will be shown when you run the .jar file.
NOTE : Do not run the .jar file from cmd using java -jar AppName.jar
Instead, just double click the .jar file and you will be seeing the images.
If it works for you, kindly upvote. :)

Image not loading in jar file

I am trying to fix this problem. Trying different solutions but nothing works. I am using NetBeans IDE. I created a project with this structure and files:
E:\java\project\ecadpb\src\ecadpb
The image files are in
E:\java\project\ecadpb\src\
I have specified working folder for my project in Netbeans as E:\java\project\ecadpb
I have created my image icons like this
new ImageIcon("device21.png");
In my source file, it works perfectly while running the project in Netbeans but images are not showing up when I build and run my JAR file separately. But the image files are inside the JAR.
I have also tried the answers for the same question asked previously.
URL imageUrl=getClass().getResource("device21.png");
new ImageIcon(imageUrl);
But it doesn't work in my case. I am building a JAR file for the first time. Can anyone help me with this!!
A simple way of doing this will be to add the image in your classpath or a directory in your classpath say img as shown below:
E:\java\project\ecadpb\src\main\java\img\device21.png
And then load your image from this location like this:
ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
URL resource = classLoader.getResource("img/device21.png");
ImageIcon icon = new ImageIcon(resource);
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.
I think answer could be one these suggestions here
I have used a similar approach,
a) Specify the package path to the image file name.
b) Make sure that the image file is not ommited by your build scripts and that it is present in your jar file.

Categories