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.
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'm making a game in NetBeans and I have to put some pictures and when I start the program they are there, but when I do clean and build pictures are not in the JAR file. Can someone help me? I'm not using JFrame form.
I am using JFrame and I think it's similar to whatever you are using while it's on netbeans
you have to put them in src folder
right click on the jLabel (if you using JFrame you have to add jLabel )
choose properties
choose icon
choose image within project
and you will find all pictures in src folder
this way when you share the JAR file the pictures will be inside it
but if you chose External image in step 5 you will use just the path of the image ( I think here's your problem )
I'm trying to make a jar, but the program needs images. When I ran the jar, the images didn't show up. However, they did in eclipse. I used this code:
label.setIcon(new ImageIcon("res/img/icon.png"));
Then I went looking on the internet for a way to fix it. I found this question here on StackOverflow, but when i tried it in my code, it's throwing a NullPointerException (also in eclipse). This is my code now:
label.setIcon(new ImageIcon(getClass().getResource("/res/img/icon.png")));
The images are in a separate folder in the package, called "res":
create a package inside your project, and name it something like "Images".
Now, add images you are using into this package.
Finally, call to these images, which are inside the package.
make sure you typed image names correctly as well
That is the best way of dealing with images
update
try
label.setIcon(new ImageIcon(getClass().getResource("res/img/icon.png")));
instead of
label.setIcon(new ImageIcon(getClass().getResource("/res/img/icon.png")));
Have a look that your RES folder is actually within the eclipse SRC folder.
Sometimes, users add a folder to the eclipse project instead.
Also:
Try cleaning and rebuilding your project.
In Eclipse, open the Navigator view. Open the /bin folder of the project. Does it have the /res/img/icon.png ( comment from here )
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.
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.