I am running net beans application, when i run it via net beans it is showing icons properly...but when i convert it to executable .exe it is not showing icons..i did a lot of research to solve this problem but everything gone in vain. This is the best method i have saw to get icons dynamically....
My icon heraricy is src/com/idoccsmain/icons/ and all of my icons inside the "icons" folder
ImageIcon icon = new ImageIcon(Main.Class.getClass().getResource("/com/idoccsmain/icons/"+"add_icon.png")
through this line i am getting all of my different icons..can any one suggest any solution for me....?
Thanks guys for responses with your help i came up with this... solution
put images in Src folder e.g src/icons/ and inside icons folder.
Than use this line in netbeans to get your images working......
ImageIcon icon = new ImageIcon(getClass().getResource("/icons/AnyIconName.png"));
and make sure use this line for each icon.
For J2ME Application
Check this image carefully, Right Click on project then Properties.
Select Application Descriptor and then select MIDlets
If it is showing anything there, like showing in this picture then edit accordingly.
NOTE : - In the above scenario src/myproject/ where icon and .java exist.
===== UPDATE =====
Desktop based executable jar
Then use, this code
this.getFrame().setIconImage(new imageIcon(getClass().getClassLoader().getResource("add_icon.png")));
Note: this line only works if the images are in the root of the jar file. If not, you have to specify the folder on the string:
getResource("yourfolder/add_icon.png")
You might also interested for this link to create executable java file and also associate icon to it.
Related
I wrote code that works whenever i run in Eclipse, but whenever i try to export it into a runnable Jar, my images dont show up.
For my images this is the code being used pic1=ImageIO.read(FourCournersRunner.class.getResource("/Images/1.png"));
When exporting the project I've chosen "Package required libraries into generated JAR":
Please also show the code where you display the image in GUI, the problem might lie in there.
Did you also package the Images folder to the .jar file?
The problem might be caused by the way you set up build path too.
In short, I don't think the code you provide right now can reproduce the problem.
I am a novice programmer trying to create a jar file that people can download and demonstrates a...JRPG-ish upgrade system I have. As a little visual pop, I want the central button to display a little icon as an extra visual pop. While it works fine in Eclipse, when I export it to a JAR file the icon is no longer displaying (though the rest of the program works as intended.
I have managed to make Eclipse import the files I want into the JAR file, as seen below.
This is the code I currently have to display them in Eclipse
imgDin = new ImageIcon("icons\Mark_of_Din.png");
imgNayru = new ImageIcon("icons\Mark_of_Nayru.png");
imgFarore = new ImageIcon("icons\Mark_of_Farore.png");
How can I modify this code to display them inside the JAR file?
You should be using
new ImageIcon(getClass().getResource("/Mark_of_Din.png"));
That little package icon in the icons folder icon, means the files in the icons folder will be at the root of the classpath. getClass() gets the calling class, and getResource() locates a resource relative to the class. The / brings the search to the root.
You can extract the jar and you will see the icons folder is not there.
See Related Post
"While it works fine in Eclipse.."
That's because when you pass a String to the ImageIcon constructor, it is looking for a file on the file system, relative to the working directory. In Eclipse (and most IDEs), the default working directory is the project root, and that's where your icons folder is.
CONTEXT FOR MY PROBLEM
I am creating a Project using java swings on netbeans IDE 7.1.2 plateform on
windows m/c.Then I'm executing the jar file created on windows m/c in a linux m/c(cent os).
In a part of my project i needed to display some pictures in run time.
PROBLEM STATEMENT
Case 1)
Now what happened is that in run time i brought those pictures from some other location to src directory of my project by executing cp(copy) command through java code.
And yes i have checked those pictures were copied inside src directory successfully.
After that when i tried to display those images in a label,pictures were not being displayed.
So i wondered why is that happening.
Case 2)
After this problem i made a change and i imported those pictures inside src folder of my project before "cleaning and building" the project on windows m/c instead of bringing them in run time on linux m/c.Then after this when i executed the jar file on linux m/c,pictures were getting displayed in label.
So only change i made was that i included the pictures in src directory during "cleaning and building" my project on windows m/c.
So what i don't understand is that
why pictures are not getting displayed in 1st case.
MY QUESTIONS AND DOUBT
1) In context to resources like files and pictures(which are being used in project and i'm not talking about source codes of project here),what exactly happens when a jar file is created ? Are those resources(files & pictures) also included in jar while building the project or not?
2) Why i'm facing such a problem ?(And i have also checked the permissions on those image files.They were exactly same in both cases.)
Any insights that any of you can provide will be deeply appreciated.
So I assume you ar doing some thing like
label.setIcon(new ImageIcon("src/path/to/image"));
or
BufferdImage img = ImageIO.read(new File("src/path/to/image"));
or worse, using absolute file references...
Netbeans will add the content of of your src directory to the resulting jar file when the project is built (excluding your source files ;)), this changes the way in which these resources need to be treated, you can no longer treat them as you would if they were files on the file system.
Once embedded within your application context (such as been included in your jar file), they become what is commonly known as embedded resources.
Instead, you need to use Class#getResource or Class#getResourceAsStream depending on your needs, for example...
label.setIcon(new ImageIcon(getClass().getResource("path/to/image")));
Updated with after more details
Image im=newImageIcon(this.getClass().getResource("/home/aman/Desktop/diya.jpg")).getImage();
Is the wrong approach for his type of file. This file is a file on the file system, instead you should be using something more like...
BufferedImage image = ImageIO.read(new File("/home/aman/Desktop/diya.jpg"));
jLabel1.setIcon(new ImageIcon(image));
Note, ImageIO.read throws an IOException, which will be expected to catch, this is useful for diagnosing missing files or bad image formats
Unless you have previously changed the visibility state of the label, there is no need to call setVisible, in fact, you're setting label invisible anyway...
I'm trying to create a (very) simple GUI program which allows the user to select from different radio buttons to display an image in a JLabel. The code I'm using is this:
JLabel imageLabel = new JLabel();
imageLabel.setIcon(new ImageIcon("images/Dog"));
My question is, where do I put this "images" folder? When I put it in the src and the bin folder, the images still didn't load. I ended up trying the following code which worked when I used the bin folder:
imageLabel.setIcon(new ImageIcon(getClass().getResource("images/Dog")));
The problem is, from what I understand the bin folder isn't supposed to be used for these files? Also, I'm pretty sure the images folder I put in there got deleted (which would be a pretty good reason not to use it...).
I'm sending the directory to my professor, so I don't want the file path to work exclusively on my computer.
PS I'm using Eclipse, if that matters.
Just put it in the project's folder. Example:
You would put your images folder where my "del_icon" image is. Just right in the folder.
I want to add images to JButtons. Here is the code I am using:
The images are in a folder called "Images", which happens to be in the source package of the application. The size of the JButtons is adjusted correctly, however no image is displayed in the button. Can someone help me solve this problem please? Thanks :)
Try create_ImageIcon(Button_NewFile, "src/Images/New File.png");
When you create an ImageIcon, it searches for relative paths where the application is launched. Normally this is in the folder where src folder is located. But have in mind that when you run it as an independent application.