Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How can I change a .jpg icon image of an icon for a jLabel using Eclipse IDE?

 
Dick Hammer
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I had done it successfully using Netbeans in this example:

http://www.coderanch.com/t/565811/GUI/java/Newbie-just-trying-change-icon

But now, I switched to Eclipse (using the WindowBuilder Pro plugin). It generates a method stub, which I filled with

JLabel lblJlabela = null;
lblJlabela.setIcon(new ImageIcon("J:\\Eclipse Folder\\Eclipse1GUI\\Photos\\Viper3.jpg"));

I had to add the first line of that because it was telling me that lblJlabela was "unresolved"...it auto-suggested that I do that. But here's the whole program.


It runs and displays a jFrame with the two initial photos, but when I press my mouse over "LabelA", nothing happens. It gives these message in console:

at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)


How can I get it to work so that I change images in that jLabel?
 
Daniel Croft
Greenhorn
Posts: 21
Eclipse IDE Java Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well right off the bat, at line 66 you're setting lblJlabela to null then trying to set an icon for it. This is obviously going to give you a null pointer exception. You want to make your jlabels into member variables of the class since you're accessing them from multiple functions; instead of declaring them in main, you should declare them at the top of your class where you declared frame.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic