0

Possible Duplicate:
How to dynamically add JLabels to JPanel?

private void jXHyperlink1MouseClicked(java.awt.event.MouseEvent evt) {
NewClass n=new NewClass();
n.myf();
try{
    ImageIcon ic=new ImageIcon("tmp/Photo0087.jpg");
    ll=new JLabel(ic,JLabel.CENTER);
    jPanel6.add(ll);
    repaint();
}catch(Exception x){
    System.out.println(x.getMessage());
   }
    }

myf() method opens up a JFileChooser in which on Pressing Open button of JFileChooser the image file gets copied to a temporary folder tmp (I copied Photo0087.jpg).

I am trying to display the file Photo0087.jpg on a JLabel for which i wrote the above code but its not working.

Community
  • 1
  • 1
c.pramod
  • 606
  • 1
  • 8
  • 22
  • We need additional code and additional information about this problem. In particular: how did you copy the file? are you sure your code to copy a file is correct? are you sure that the "tmp" folder is in the same folder where your program is? – BackSlash Jan 12 '13 at 17:10
  • Why are you dynamically adding the label as opposed to adding it at start-up & setting the icon (e.g as seen in [`ImageViewer`](http://stackoverflow.com/a/13463684/418556)) in the mouse event method? – Andrew Thompson Jan 12 '13 at 17:17

2 Answers2

1

You need to revalidate the JPanel after adding the new JLabel:

jPanel6.revalidate();
Reimeus
  • 158,255
  • 15
  • 216
  • 276
0

Either pass Absolute image path like this C:\\folder\\folder2\\image.jpg

or try this ImageIcon ic = new ImageIcon(getClass().getResource("image location"))

exexzian
  • 7,782
  • 6
  • 41
  • 52