(First of all, I'm sorry If there is like a million questions like this. I've tried everything I can and this is driving me nuts!)
I'm trying to had an icon to a JButton but I keep getting an IllegalArgumentException caused by ImageIO.
Here's what I have:
//Other UI elements ^
JButton X = new JButton("Clear");
//com.oliveira.ux is the package name
Image img = ImageIO.read(getClass().getResource("/com.oliveira.ux/resource/gtk-clear.png"));
Icon clear = new ImageIcon(img);
//More UI elements
The Icon is located under src/PACKAGE NAME/resource/. (I use eclipse).
I've tried to change the location on the code above (the one I wrote here was the last one I tried) but all I get is an IllegalArgumentException when I run de .jar. Any suggestions?
Many thanks in advance
Here's the full error message:
Caused by: java.lang.IllegalArgumentException: input == null!
at javax.imageio.ImageIO.read(Unknown Source)
at com.oliveira.ux.Main.<init>(Main.java:146)
at com.oliveira.ux.Main.main(Main.java:75)
... 5 more
This points to the ImageIO part in the code I wrote above.
The path appears to be wrong...
Image img = ImageIO.read(getClass().getResource("/com.oliveira.ux/resource/gtk-clear.png"));
getResource isn't expecting the pack name, but the "path" to the resource from the context of the class path (so the path is appended to the class path elements)
Something like...
Image img = ImageIO.read(getClass().getResource("/com/oliveira/ux/resource/gtk-clear.png"));
Should give a better result
Related
Sorry newbie here. I get an error in Netbeans while trying to add an Icon to a J label. The error is:
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException: Cannot invoke "java.net.URL.toExternalForm()" because "location" is null
After importing all of the images I used for my project. I still get this error.
This is the code in default in Netbeans:
diceButton = new javax.swing.JButton();
diceButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/resources/dice.png"))); // NOI18N
(https://i.stack.imgur.com/VCANk.png)
I'm expecting to add it to a jlabel so that the image will also be at the jar file
You should also try this method to better understand label and button's icon (image) property.
Using absolute path, like my image in D drive in img folder,
ImageIcon icon = new ImageIcon("D:\\img\\img-name.jpg");
img_label.setIcon(icon);
Or you can also do this from label/image icon- property by specifying absolute path.
I'm new at Java and also swing. I just created a small app using JFrame and added some buttons and textFields, also I have a method which set the icon that I want for the taskbar and the one in the left corner.
When I run the program in Netbeans everything seems correctly, but when I build the project the icon it's not showing up. I tried a lot of things but none of them worked for me.
here's the method that I use for the program:
private void setIcon() {
ImageIcon imageIcon = new ImageIcon("src/main/java/icons/steam.png");
this.setIconImage(imageIcon.getImage());
}
And I call the method from the constructor.
Thank you.
EDIT 1:
Implementing what Andrew said, now I have this:
BufferedImage img = null;
try {
URL url = getClass().getResource("src/main/java/icons/steam.png");
img = ImageIO.read(url);
} catch (IOException e) {
}
this.setIconImage(img);
And that's on the constructor. But when I run it I get:
Exception in thread "AWT-EventQueue-0" java.lang.IllegalArgumentException: input == null!
at java.desktop/javax.imageio.ImageIO.read(ImageIO.java:1400)
I tried different paths but I can't get it. And yes, I'm sure that "steam.png" is there.
I been looking for a solution to this and I finally get it (thanks to Andrew trough the comments)
First I edited my code as you can see in EDIT 1
After that, I got an IllegalArgumentException and the problem was that I didn't have a "resources" folder under /src
So I created my resources folder under src/main/resources and put my image inside
Then I got it using
URL url = getClass().getResource("/icons/steam.png");
img = ImageIO.read(url);
And that was the fix for my problem, now when I run the program images are now loaded.
Thank you so much!
I'm developing a cross platform desktop application using Jython and Swing, and I found a hitch.
I would like to develop a button that allows me to load an image in its background and to change it when I reclick on the image loaded.
I attach as an example some pictures on how I would like my widget to be.
Upload pane without image
And then, when I load an image:
Upload Pane with image
I tried with the following code:
fd = FileDialog(self, "Scegli un'immagine", FileDialog.LOAD)
fd.setFile(';*.'.join(("*.bmp","jpg","jpeg","wbmp","png","gif")))
fd.setVisible(True)
fileName = fd.getFile()
if fileName != None :
fileAbsPath = os.path.abspath(fileName)
"""'self.bigDict['imgButton']' is the current JButton"""
self.bigDict['imgButton'].setIcon(ImageIcon(fileAbsPath))
When I click on "Open", the image is not inserted into the button. I do not understand why.
I also tried with the following code:
if fileName != None :
fileAbsPath = os.path.abspath(fileName)
img = ImageIO.read(getClass().getResource(fileAbsPath))
self.bigDict['imgButton'].setIcon(img)
In the above example the following error is reported:
img = ImageIO.read(getClass().getResource(fileAbsPath))
TypeError: getClass(): expected 1 args; got 0
I would be curious to know why the button is not updated with the image loaded, and why the above error in java would not happen.
Thanks in advance!
getClass() needs 1 argument: the implicit this argument. You must call the method on an object, or use the MyClass.class notation.
The problem is very simple.
When an image is loaded with FileDialog it is "virtually located" in the FileDialog window, but the image does not exist. I realized this when I tried to copy the image from the absolute path to the destination folder, using shutil.copy2(self.imgAbsPath, destinationPath+'/'+self.imgName), which reported an error message indicating that the image does not exist.
To provide the concrete path of a file, it is necessary to add information about the folder where it is located.
In practice you have to create the relative path, before generating the absolute one:
fd = FileDialog(self, "Scegli un'immagine", FileDialog.LOAD)
fd.setFile(';*.'.join(("*.bmp","jpg","jpeg","wbmp","png","gif")))
fd.setVisible(True)
self.imgName = fd.getFile()
relativePath = fd.getDirectory() + fd.getFile() """Here is the missing piece!"""
if self.imgName != None:
self.imgAbsPath = os.path.abspath(relativePath) """Here the absolute path is correctly obtained!"""
self.bigDict['imgButton'].setIcon(ImageIcon(ImageIcon(self.imgAbsPath).getImage().getScaledInstance(130, 130, Image.SCALE_DEFAULT)))
"""The image, before being inserted into the button, is slightly resized, to avoid graphic bumps!"""
I hope I've been helpful.
I was following a reference and some tutorial on setting the path of an image for a JLabel but can't get the path correctly.
I have
String pathToUserPictureImage = "resources/Assets/studentPhoto.png";
ImageIcon userPicture = new ImageIcon(getClass().getClassLoader().getResource(pathToUserPictureImage));
JLabel userPictureImagePlaceHolder = new JLabel(userPicture, HEIGHT);
And my folders in the project are:
When I run it, it would return an error message about a Null Pointer which I suspect to be a problem with the path supplied.
I would appreciate any suggestion and correction to my code. I recently just switched from dragging and dropping components to writing code by hand.
I have 6 JButtons on my GUI all have images on it,
when I compile and run the code, all images on JButtons show up perfectly
but in runnable JAR file, images on JButtons are not showing up.. how do I fix this problem?
I used this method in my code to show icons on JButtons
ImageIcon SettingsIc = new ImageIcon("bin/images/settings.png");
jb1 = new JButton(SettingsIc);
jb1.setFocusPainted( false );
//jb1.setBorderPainted(false);
jb1.setContentAreaFilled(false);
This is how my GUI looks when I compile my code in Eclipse
This is how my GUI looks after executing Runnable JAR file
This (as pointed out by a number of people)
ImageIcon SettingsIc = new ImageIcon("bin/images/settings.png");
Suggests that you are trying to load the images from the bin/images off the file systems. This is a relative path from the execution point of your application.
ImageIcon won't complain if the file does not exist.
If possible, you are better off embedding the resources within your Jar file (it will make it easier to deploy) and use something like getClass().getResource("/bin/images/settings.png") to load the images.
If possible, you should try using ImageIO.read(URL) to load your images, it will throw an exception if the resource pointed to by the File/URL does not exist (or is invalid).
Just keep the jar and images in the same folder and
keep
ImageIcon icon = new ImageIcon("image.jpg");
in the code