Java Forum

Ask Question   UnAnswered
Home » Forum » Java       RSS Feeds

ImageIcon("pic.gif") = no image?

  Asked By: Navin    Date: Apr 14    Category: Java    Views: 2572

I must be an idiot. I am working my way through a tutorial


and everything was going fine until the part where we add an image
and some text to a JLabel. The text is displaying fine, the image is
simply not displaying. No error on compile, just no image.

Yes, I have verified the image is in the same folder as the .class
file...code is:

package divelog;
* This class creates the content on the
* Welcome tabbed pane in the Dive Log
* application.
* @version 1.0

import javax.swing.*; //imported for buttons, labels, and images
import java.awt.*; //imported for layout manager

public class Welcome extends JPanel
{ //Opening class

// Variables for objects
private JLabel jl;
private JTextArea ta;
private JLabel diver;

public Welcome()
{ // Opens constructor

jl = new JLabel("test", new ImageIcon("diveflag.gif"), JLabel.CENTER);
jl.setFont(new Font("Times-Roman",
Font.BOLD, 17));

setLayout(new BorderLayout());

} // Closes constructor

}//Closes class Welcome

//end code

The text behaves as expected - but no image at all.

What in the world am I doing wrong? This seems like such a simple



2 Answers Found

Answer #1    Answered By: Garai Chalthoum     Answered On: Apr 14

Actually, since your .class file is probably contained in a divelog directory,
you'd want the image  to go in the parent directory.

Answer #2    Answered By: Caitlin Brown     Answered On: Apr 14

If you are using an IDE like Jbuilder (or something
similar I assume) the IDE creates two (three with
backup) kindof nonstandard folders that might differ
from your tutorial (I haver not checked it). For
example JBuilder has one folder  with its class  files
and one with its source files but no folder for pic
files. You shold put your pictures in the source
folder and JBuilder will automatically transfer them
into the "executables" or class directory or to be
absolutely sure (or if you have changed the image) put
a copy of it in both the directories). If you use
another IDE check where the source code  xxx.java files
are located or if no IDE or if a very simple IDE like
Kawa that works like a text  editor is present check
for the combined .class and .java folder and put your
image there, as far as I know only .gif and .jpg.
Later make sure that you put them into the .jar or
package etc but I guess you're not there yet.

Didn't find what you were looking for? Find more on ImageIcon("pic.gif") = no image? Or get search suggestion and latest updates.