PDA

View Full Version : Java ImageIcon not displaying



DBQ
April 11th, 2008, 11:22 PM
Hi everybody.

I have another strange problem. I am using java JpopupMenu. I want my menu to have an icon next to the text. For some reason the ImageIcon object finds the file, but does not display the image. Note: I tried jpg, gif, and png. None are displaying.

Any ideas?

kostkon
April 12th, 2008, 12:08 AM
Are you sure that you are putting the right path as an argument, e.g.:

ImageIcon testicon=new ImageIcon("../../images/popup_icon.gif");

DBQ
April 12th, 2008, 12:12 AM
I am including the right path - the object finds it.

kostkon
April 12th, 2008, 12:28 AM
I am including the right path - the object finds it.
OK, I see.

Hmmm, you could try to set the position of the text relative to the icon with the setHorizontalTextPosition(int textPosition) method that a JMenuItem object inherits from javax.swing.AbstractButton.

Maybe the text blocks the icon from appearing, it does not leave space for it. Thus, after you create the menu items, call the setHorizontalTextPosition() accordingly, e.g.:

testItem.setHorizontalTextPosition(JMenuItem.RIGHT );

DBQ
April 12th, 2008, 12:30 AM
I figured it out. Apperently when using eclicpse, you must also include the image to the resource list. Thanks for trying to help though.

xlinuks
April 12th, 2008, 12:52 PM
If your project/test will have many menu icons/images make sure to load them in a separate thread and apply them wisely when ready, if failed loading image provide a custom painted "no-image" BufferedImage for your GUI to work even if resources not found. This will also protect you from issues on windows/Linux you might get weird/different behaviour because of asynchronous image loading.
If you didn't run into such issues yet, you will run into this sooner or later as the gui gets stuffed.