Java adding ImageIcon to JLabel
Asked Answered
P

3

8

I am trying to make a very basic game with Java and I am having trouble displaying an image on a JFrame. It has worked in the past for me and now is not, i can't see what I did wrong.

I have tried printing the current working directory and changing where I get my image to match that. It is likely that the problem is not getting the image, since my (filefinder or filereader or something like that) can find it without problems, but I cannot correctly add it (the ImageIcon) to the JLabel, or that to the JFrame.

This is my code...

JFrame frame = new JFrame("no image");
ImageIcon image = new ImageIcon("C:/Documents and Settings/user/Desktop/hi/xD/JavaApplication2/image.png");
JLabel imagelabel = new JLabel(image);
frame.add(imagelabel);

The JFrame has been setVisible(true) and pack().

Could someone please help me understand what is wrong.

Prot answered 28/6, 2012 at 11:35 Comment(8)
Please have a look at this example, How to add images to your Project or Please follow these stepsEating
I looked at those examples, they did not helpProt
Simply place your image next to your .class file, and use it like this ImageIcon image = new ImageIcon(getClass().getResource("yourImage.extension"));. That link has to work, since it's the right way to put your images in your project. I hope you had walked through all the steps mentioned in that !!Eating
Simply copy the nicely idented code from your notepad to the question area, then select all the code and press CTL + K, to format it. You don't really have to press space bar eight times for each line.Eating
that throws a nullpointer exceptionProt
I made it work myself, the solution was to name the images "New Bitmap Image". I think that for some reason it thought i didn't specify a file name, and was looking for the default filename.Prot
Glad you got it sorted, KEEP UP the GOOD WORK :-)Eating
how to i close this question now that i have solution?Prot
Z
12

Your problem lies here:

   ImageIcon image = new ImageIcon("C:/Documents and Settings/user/Desktop/hi/xD/JavaApplication2/image.png");
   JLabel imagelabel = new JLabel(character);

You create an ImageIcon "image" but you create your JLabel with "character".

It should be:

JLabel imagelabel = new JLabel(image);
Zibet answered 28/6, 2012 at 11:41 Comment(1)
sorry, in my actual code they are the same, I forgot to replace character with image.Prot
T
2

Try,

ImageIcon image = new ImageIcon("c:\\path\\image.png");
imagelabel = new JLabel(character, image, JLabel.CENTER);
frame.add(imagelabel);

Take a look at Tutorial - How to use Icons

Tyre answered 28/6, 2012 at 11:42 Comment(1)
I have looked at the tutorial many times, I don't see what i did wrong.Prot
L
-1
import javax.awt.*; 
import java.awt.*; 
import java.awt.event*; 

//class name image 
class image { 
    image() 
    //constructor { 
        Frame f=new Frame("Image"); 
        //Frame
        f.setSize(500,500); 
        f.setVisible(true); 
        Panel p =new Panel(); 
        //Panel 
        f.add(p); 
        p.addLayout(null); 
        ImageIcon ii=new ImageIcon("set your image path"); 
        //ImageIcon is used to image Display . 
        Label l =new Label(ii); 
        p.add(ii); 
        p.setBounds(set you bounds); 
        //Like that(20,20,500,40); 
    } 

    public static void main(String [] args) { 
        image obj = new 
    } 
}
Levi answered 29/4, 2015 at 5:55 Comment(1)
Please add some explanations about your code and about that why OP needs your code ;).Mirella

© 2022 - 2024 — McMap. All rights reserved.