Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

IMAGE 1

Status
Not open for further replies.

eh171

Programmer
Feb 21, 2003
9
0
0
GB
I am trying to insert a image to a panel as follows but it does not work:

ImageIcon image = new ImageIcon("image.gif");
panel.add(image,BorderLayout.EAST);

p.s. the image is in the same location as the java file
 
>> it does not work:

?? You get exception message or what?

-pete
 
I had the same code and same problem. The code compiled fine without any errors but image did not show up in my case. Any hint appreciated.
 
Is it an Applet? Are you looking in the Applet console for exceptions?

-pete
 
This one will work

import java.awt.*;
import javax.swing.*;
public class Driver
{
public static void main(String[]arg)
{
JFrame frame = new JFrame();
Container cont = frame.getContentPane();
JPanel panel = new JPanel();
JLabel i = new JLabel(new ImageIcon("mail.gif"));

panel.add(i);

cont.add(panel);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);

}

}
 
No. I am not doing an applet. I have tried Fredrick's code above(thanks!) but result is the same. No error and no image. This puzzles me. I am using Sun One Studio CE. Would it have to do with this problem? Thanks for any help.
 
uhm... i cant remember off hand but it is something like

Image myImage;

forgotthis = Toolkit.getDefaultToolkit().getImage("filename.extention");

myImage = new Image(forgotthis);
panel.add(myImage);

i forgot what type forgotthis was ... but that is how you do it... i can look later, drop me an email at bum0472@hotmail.com and ill tell you exactly what it is.


 
try this code and see if it works.

import java.awt.*;
import javax.swing.*;
public class Driver
{
public static void main(String[]arg)
{
JFrame frame = new JFrame();
Container cont = frame.getContentPane();
JPanel panel = new JPanel();
JLabel i = new JLabel(new ImageIcon("mail.gif"));

panel.add(i,null);
cont.add(panel);
panel.setVisible(true);

frame.setDefaultCloseOperation JFrame.EXIT_ON_CLOSE);
// set the size of the frame
frame.setVisible(true);
frame.pack();

}

}
 
as you are using Sun One Studio it is probably looking for the image in the root directory of Sun One. Load images relative to a class resource, this will also allow you to put images into a jar file along with your classes.

e.g. ImageIcon icon = new Imageicon( getClass().getResource("images/document.gif") )

or use the Toolkit as mentioned above by comet345

Jeremy Nicholson, Director of a UK-based Java and Data Warehousing consultancy
 
Thanks! Jeremy
getClass().GetResource() did the job.

Smin.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top