9 Replies - 6995 Views - Last Post: 09 October 2012 - 01:19 PM Rate Topic: -----

#1 Mavsstarter21  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 42
  • Joined: 12-May 11

Images in Eclipse IDE

Posted 11 June 2011 - 04:00 PM

I have never been able to display images in any program that I compile and run with Eclipse.

Here is the current example:

1.
ImageIcon backArrowIcon = new ImageIcon("agt_back-64.png");
    		jBackButton.setIcon(backArrowIcon);


I have also tried:

2.
jBackButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("agt_back-64.png")));


This one gives me a null pointer exception.

"agt_back-64.png" is in the same package/folder as this class. I have tried putting it in its own image folder. I have changed "agt_back-64.png" to include some or all of its directories. I have even opened a form editor in eclipse and tried to invoke the setIcon() from the form editor using its file chooser, but no luck.

The thing is, I tried this in NetBeans earlier today and it worked perfectly. I even copy and pasted the exact line of code I used in NetBeans and replaced the file directory, but I still get a null pointer. Does anyone have any possible solutions or diagnosis?

Or maybe someone has an alternative way to import images into Eclipse or something?

Here's the exception I'm getting if it helps:

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
	at javax.swing.ImageIcon.<init>(Unknown Source)
	at s.Clone.getJButton8(Clone.java:1639)
	at s.Clone.getJPanel8(Clone.java:1609)
	at s.Clone.getJDialog3(Clone.java:1597)
	at s.Clone.<init>(Clone.java:1231)
	at s.Clone$5.run(Clone.java:1251)
	at java.awt.event.InvocationEvent.dispatch(Unknown Source)
	at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
	at java.awt.EventQueue.access$000(Unknown Source)
	at java.awt.EventQueue$1.run(Unknown Source)
	at java.awt.EventQueue$1.run(Unknown Source)
	at java.security.AccessController.doPrivileged(Native Method)
	at java.security.AccessControlContext$1.doIntersectionPrivilege(Unknown Source)
	at java.awt.EventQueue.dispatchEvent(Unknown Source)
	at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
	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)


Is This A Good Question/Topic? 0
  • +

Replies To: Images in Eclipse IDE

#2 Dogstopper  Icon User is offline

  • The Ninjaducky
  • member icon



Reputation: 2874
  • View blog
  • Posts: 11,047
  • Joined: 15-July 08

Re: Images in Eclipse IDE

Posted 11 June 2011 - 04:15 PM

How did you add the images? Did you use the File->Import feature? If not, try using that.
Was This Post Helpful? 0
  • +
  • -

#3 Mavsstarter21  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 42
  • Joined: 12-May 11

Re: Images in Eclipse IDE

Posted 11 June 2011 - 06:28 PM

View PostDogstopper, on 11 June 2011 - 04:15 PM, said:

How did you add the images? Did you use the File->Import feature? If not, try using that.


Just tried it, no luck. It made a copy of the already existing .png file.
Was This Post Helpful? 0
  • +
  • -

#4 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8342
  • View blog
  • Posts: 31,880
  • Joined: 06-March 08

Re: Images in Eclipse IDE

Posted 11 June 2011 - 09:13 PM

View PostDogstopper, on 11 June 2011 - 07:15 PM, said:

How did you add the images? Did you use the File->Import feature? If not, try using that.

@Dogstopper, that would make a difference if you jar but not if you run from Eclipse :)

@OP: Does you .java and .class in the same folder ? Not the default for Eclipse.
Make sur your .png is in the same folder as your .class.
Do a System.out.println(backArrowIcon);
if it does not print "null" it means the read was done correctly, the problem is somewhere else.

What is your class "s" ? Does it implement cloneable ?
Was This Post Helpful? 1
  • +
  • -

#5 Mavsstarter21  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 42
  • Joined: 12-May 11

Re: Images in Eclipse IDE

Posted 12 June 2011 - 12:14 AM

View Postpbl, on 11 June 2011 - 09:13 PM, said:

View PostDogstopper, on 11 June 2011 - 07:15 PM, said:

How did you add the images? Did you use the File->Import feature? If not, try using that.

@Dogstopper, that would make a difference if you jar but not if you run from Eclipse :)

@OP: Does you .java and .class in the same folder ? Not the default for Eclipse.
Make sur your .png is in the same folder as your .class.
Do a System.out.println(backArrowIcon);
if it does not print "null" it means the read was done correctly, the problem is somewhere else.

What is your class "s" ? Does it implement cloneable ?


System.out.println(backArrowIcon) returned agt_back-64.png.
No, I do not believe class "s" implements cloneable.
Was This Post Helpful? 0
  • +
  • -

#6 Mavsstarter21  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 42
  • Joined: 12-May 11

Re: Images in Eclipse IDE

Posted 12 June 2011 - 12:24 AM

The s.class is in the bin where the .png is now. No exception occurred this time. It looks like this may be the solution. I'll keep working on getting the icon to display, and thanks pbl.
Was This Post Helpful? 0
  • +
  • -

#7 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8342
  • View blog
  • Posts: 31,880
  • Joined: 06-March 08

Re: Images in Eclipse IDE

Posted 12 June 2011 - 11:32 AM

View PostMavsstarter21, on 12 June 2011 - 03:14 AM, said:

System.out.println(backArrowIcon) returned agt_back-64.png.
No, I do not believe class "s" implements cloneable.

So your .png is correctly read. The problem is somewhere else.

The question about cloneable is because you stack trace starts in the clone() method of class s.
Was This Post Helpful? 0
  • +
  • -

#8 rvivekdash  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 09-October 12

Re: Images in Eclipse IDE

Posted 09 October 2012 - 12:30 PM

View PostMavsstarter21, on 11 June 2011 - 04:00 PM, said:

I have never been able to display images in any program that I compile and run with Eclipse.

Here is the current example:

1.
ImageIcon backArrowIcon = new ImageIcon("agt_back-64.png");
    		jBackButton.setIcon(backArrowIcon);


I have also tried:

2.
jBackButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("agt_back-64.png")));


This one gives me a null pointer exception.

"agt_back-64.png" is in the same package/folder as this class. I have tried putting it in its own image folder. I have changed "agt_back-64.png" to include some or all of its directories. I have even opened a form editor in eclipse and tried to invoke the setIcon() from the form editor using its file chooser, but no luck.

The thing is, I tried this in NetBeans earlier today and it worked perfectly. I even copy and pasted the exact line of code I used in NetBeans and replaced the file directory, but I still get a null pointer. Does anyone have any possible solutions or diagnosis?

Or maybe someone has an alternative way to import images into Eclipse or something?

Here's the exception I'm getting if it helps:

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
	at javax.swing.ImageIcon.<init>(Unknown Source)
	at s.Clone.getJButton8(Clone.java:1639)
	at s.Clone.getJPanel8(Clone.java:1609)
	at s.Clone.getJDialog3(Clone.java:1597)
	at s.Clone.<init>(Clone.java:1231)
	at s.Clone$5.run(Clone.java:1251)
	at java.awt.event.InvocationEvent.dispatch(Unknown Source)
	at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
	at java.awt.EventQueue.access$000(Unknown Source)
	at java.awt.EventQueue$1.run(Unknown Source)
	at java.awt.EventQueue$1.run(Unknown Source)
	at java.security.AccessController.doPrivileged(Native Method)
	at java.security.AccessControlContext$1.doIntersectionPrivilege(Unknown Source)
	at java.awt.EventQueue.dispatchEvent(Unknown Source)
	at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
	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)


I had the same problem with ImageIcon, I couldn't add an png image in my menubar.
Here's what i did to solve the problem..
create a new folder named 'res' along with 'src' and 'bin' folders in your project folder..
copy and paste the image file in the 'res' folder..
go to eclipse and refresh your project..
After refresh, you should be able to see the folder and the image file in the package explorer..
run the code and test it..

Good Luck :)
Was This Post Helpful? 0
  • +
  • -

#9 g00se  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 2781
  • View blog
  • Posts: 11,768
  • Joined: 20-September 08

Re: Images in Eclipse IDE

Posted 09 October 2012 - 12:57 PM

See http://technojeeves....va-with-eclipse
Was This Post Helpful? 0
  • +
  • -

#10 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8342
  • View blog
  • Posts: 31,880
  • Joined: 06-March 08

Re: Images in Eclipse IDE

Posted 09 October 2012 - 01:19 PM

rvivekdash please avoir necroposting 4 months old posts
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1