0

Can anyone tell me why (by giving me the code) to make the image Dots replace with JLabel Hi so Dots shows up. All I am getting right now is the words Hi.

public class Memory extends JPanel {     
ImageIcon Dots = new ImageIcon("Dots.jpg");
private JLabel Hi = new JLabel("Hi");
//ImageIcon Dots = new ImageIcon(this.getClass().getResource("Dots.jpg"));

Memory(){   
    Hi.setIcon(Dots);
    Hi.setPreferredSize(new Dimension(100,100));
    this.add(Hi);
}
}
Roman C
  • 49,761
  • 33
  • 66
  • 176
user2686522
  • 17
  • 1
  • 2
  • 1
    variable/members within a class should start with a LowerCase letter. Regarding your problem, is your image `Dots.jpg` path correct? It is at the same level as your class `Memory`? – araknoid Aug 19 '13 at 14:48
  • 1
    Please have a look at this [answer](http://stackoverflow.com/a/9866659/1057230). Though if you doing it manually see the last link in that answer. Hope it helps :-) – nIcE cOw Aug 19 '13 at 14:53
  • Or you can take some help from this answer of mine, regarding [set Icon for JLabel](http://stackoverflow.com/a/15187181/1057230) :-) – nIcE cOw Aug 19 '13 at 18:14

1 Answers1

1

You should create a folder inside your "src" folder (the folder containing all your .java classes/code) and then put your *.jpg icons there. Then try something like this

hi.setIcon(new javax.swing.ImageIcon(getClass().getResource("/resources/Dots.jpg"))); 

Assuming that the folder's name you created it's "resources".

Abstract
  • 664
  • 1
  • 5
  • 15
  • I tried doing what nivde92 suggested. It says, Applet not initialized. – user2686522 Aug 19 '13 at 15:02
  • It worked?. I had an extra folder inside resources called "jpg", check the code now, it shoud work. – Abstract Aug 19 '13 at 15:04
  • I'm confused. It says applet not initialized and this error at sun.reflect.Reflection.ensureMemberAccess(Unknown Source) at java.lang.Class.newInstance(Unknown Source) at sun.applet.AppletPanel.createApplet(Unknown Source) at sun.applet.AppletPanel.runLoader(Unknown Source) at sun.applet.AppletPanel.run(Unknown Source) at java.lang.Thread.run(Unknown Source) – user2686522 Aug 19 '13 at 15:09
  • could you upload a screen shot from the folders you had inside your project? – Abstract Aug 19 '13 at 15:13
  • i have the screenshots, but how to i upload them to this website – user2686522 Aug 19 '13 at 15:26
  • try uploading to another website and then just share the link – Abstract Aug 19 '13 at 15:41
  • basically- the src folder. in the src folder is the resources folder. in the resources folder is the jpg folder. in the jpg folder is the image shortcut, Dots.jpg – user2686522 Aug 19 '13 at 16:29
  • 1
    hmm image shorcut? put the actual image on the folder – Abstract Aug 19 '13 at 17:02