File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Swing / AWT / SWT and the fly likes How to paste a image on a frame Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "How to paste a image on a frame" Watch "How to paste a image on a frame" New topic
Author

How to paste a image on a frame

ankur rathi
Ranch Hand

Joined: Oct 11, 2004
Posts: 3830
Hi all ,
I m trying to paste an image on a frame .

Paster() {

setSize(500,500);
Container con = getContentPane();
Icon i = new ImageIcon("c:/hi.gif");
JLabel l = new JLabel(i);
con.add(l);
}

But its not working .
This is a constructor of a class extending JFrame .

Please let me know what wrong in this .
Also provide some code .
Thanks in advance .
Edward Chen
Ranch Hand

Joined: Dec 23, 2003
Posts: 798
you almost get it. we need add the contentpane to JFrame.

ImageIcon icon = new ImageIcon("ddd.gif");
JLabel label = new JLabel();
label.setIcon(icon);
frame.getContentPane().add(label);
frame.setSize(300,300);
frame.setVisible(true);
Pat Hays
Ranch Hand

Joined: Aug 20, 2004
Posts: 138
I guess you must add your JLabel after the JFrame setVisible(true). If so, you should call validate() to show this JLabel.

Change your code as below:
Paster() {
setSize(500,500);
Container con = getContentPane();
Icon i = new ImageIcon("c:/hi.gif");
JLabel l = new JLabel(i);
con.add(l);
this.validate();
}


Download Java GUI Builder, <a href="http://www.mars3000.com" target="_blank" rel="nofollow">http://www.mars3000.com</a>
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to paste a image on a frame