*
The moose likes Swing / AWT / SWT and the fly likes How can I change a .jpg icon image of an icon for a jLabel using Eclipse IDE? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "How can I change a .jpg icon image of an icon for a jLabel using Eclipse IDE?" Watch "How can I change a .jpg icon image of an icon for a jLabel using Eclipse IDE?" New topic
Author

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

Dick Hammer
Greenhorn

Joined: Jan 28, 2012
Posts: 18
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

Joined: Feb 16, 2012
Posts: 21

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.
 
 
subject: How can I change a .jpg icon image of an icon for a jLabel using Eclipse IDE?
 
Similar Threads
Helped needed: JBuilder 9 Display a .jpg image
trouble displaying ImageIcon
four in a row rmi game
jpeg in a Jpanel
Display an image for a set time