WindowBuilder ImageIcon - java

Just a rather simple step but I can't get it done.
A code like this:
label.setIcon(new ImageIcon ("abc.JPG"));
Problem:
ImageIcon cannot be resolved to a type
What's wrong there? Is there any libraries or configuration that I missed out?

Related

Program icon not showing up after Clean and Build (Netbeans)

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!

How To Correctly Specify Image path from a folder (ImageIcon)

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.

Jdialog box Title bar icon change

I need to change the Jdialog box title bar icon. By default it uses a java coffee image.
I have searched in internet and used many codes
1. Image im = Toolkit.getDefaultToolkit().getImage("/org/qmon/generate/Images/JDialog -2.ico");
dialog.setIconImage(im);
2. Toolkit kit = Toolkit.getDefaultToolkit ();
Image img = kit.getImage ("/org/qmon/generate/Images/Create File Tag-16x16.png");
dialog.setIconImage(img);
nothing works properly.. Kindly help me.. Thanks in Advance
Firtsly, ico is not a support image format for Java.
The likely reason you're having issues with the second approach is that getImage is expecting a file reference and the image you seem to referencing looks like it's embedded (stored within your application)
Try using something more like...
Image img = kit.getImage (getClass().getResource("/org/qmon/generate/Images/Create File Tag-16x16.png"));
Instead.
Personally, I prefer ImageIO.read as it throws a IOException when something goes wrong...
Image img = ImageIO.read(getClass().getResource("/org/qmon/generate/Images/Create File Tag-16x16.png"));
But that's me...
You should also consider taking a look at Convert List<BufferedImage> to Image which demonstrates the use of ico file (from a 3rd party API) and setIconImages method
Image image = ImageIO.read(new URL(
"http://www.gravatar.com/avatar/f1d58f7932b6ae8027c4e1d84f440ffe?s=128&d=identicon&r=PG"));
dialog.setIconImage( image );
dialog.setVisible(true);
I am using this in my application and working fine
java.net.URL url = ClassLoader.getSystemResource("res/java.png");
ImageIcon icon = new ImageIcon(url);
JOptionPane.showMessageDialog(null, jep, "UroSync",JOptionPane.INFORMATION_MESSAGE, icon);
To improve what MadProgrammer has said, I met the problem and I solved it instantiating a JDialog but using the static class Toolkit method getDefaultToolkit().getImage(Image img).
JDialog dialog = new JDialog();
dialog.setIconImage(Toolkit.getDefaultToolkit().getImage(MyMainClass.class.getResource("/myIcon.png")));
To do that you need to add before the image into the build path of the Project.

error adding image object

The following code produces error:
ImageIcon i=new ImageIcon("logo.png");
Image scaleImage=i.getImage().getScaledInstance(10,10,Image.SCALE_DEFAULT);
mainPanel.add(scaleImage);
The error is cannot find method add(Image).
Why it is giving this error?
You can't do it like that. JPanel does not accept Image as parameter (that's what the error tells you).
You have two options:
draw the image in the panel instead. The solution uses the paintComponent(..) method.
use a JLabel and an ImageIcon
The answers to this question will show you how to do it either way.
ImageIcon i=new ImageIcon("logo.png");
Image scaleImage=i.getImage().getScaledInstance(70,70,Image.SCALE_DEFAULT);
ImageIcon ii=new ImageIcon(scaleImage);
JLabel pic=new JLabel(ii);
mainP.add(pic); // now you can add

path of an image in an eclipse project

I'm trying to display two pictures on my JFrame, the way I found was to use icons and JLabels, this seems pretty straightforward and I'm not having problems with this. But when it comes to locating the image I can't get it to work. I'm on a linux machine thus the forwardslash style. I created a folder called pics in my project which is called 399assig1.
ImageIcon icon1 = createImageIcon("/home/dsk03/ugrad/jeanbern/workspace/C291/workspace/399assig1/pics/fur-05.jpg","First");
this.label1 = new JLabel("Picture 1", icon1, JLabel.CENTER);
ImageIcon icon2 = createImageIcon("pics/fur.png","Second");
this.label2 = new JLabel("Picture 2", icon2, JLabel.CENTER);
this is the error I get
Couldn't find file: /home/dsk03/ugrad/jeanbern/workspace/C291/workspace/399assig1/pics/fur-05.jpg
Couldn't find file: pics/fur.png
if createImageIcon() is searching the CLASSPATH for the file, you'd need to add the root directory to the CLASSPATH. A better approach would be use a path that is relative to a directory that is already included in the CLASSPATH.
Like so:
%>CLASSPATH=$CLASSPATH;/home/dsk03/ugrad/jeanbern/workspace
then your call would be
ImageIcon icon1 = createImageIcon("399assig1/pics/fur-05.jpg", "MyIcon");

Categories