473,473 Members | 2,159 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

ImageIcon not showing

3 New Member
Hello, I am following a tutorial but have a problem.
With the following code the icons will not appear unless I specify the entire path to where they are located, I cant work out why it wont work, what am I missing?

Thanks for any help

Expand|Select|Wrap|Line Numbers
  1. public class IconFrame extends JFrame {
  2.     JButton load, save, subscribe, unsubscribe;
  3.  
  4.     public IconFrame() {
  5.         super("Icon Frame");
  6.         setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  7.         JPanel panel = new JPanel();
  8.         // create icons
  9.  
  10.         ImageIcon loadIcon = new ImageIcon("load.gif");
  11.         ImageIcon saveIcon = new ImageIcon("save.gif");
  12.         ImageIcon subscribeIcon = new ImageIcon("subscribe.gif");
  13.         ImageIcon unsubscribeIcon = new ImageIcon("unsubscribe.gif");
  14.         // create buttons
  15.         load = new JButton("Load", loadIcon);
  16.         save = new JButton("Save", saveIcon);
  17.         subscribe = new JButton("Subscribe", subscribeIcon);
  18.         unsubscribe = new JButton("Unsubscribe", unsubscribeIcon);
  19.         // add buttons to panel
  20.         panel.add(load);
  21.         panel.add(save);
  22.         panel.add(subscribe);
  23.         panel.add(unsubscribe);
  24.         // add the panel to a frame
  25.         add(panel);
  26.         pack();
  27.         setVisible(true);
  28.     }
  29.  
  30.     public static void main(String[] arguments) {
  31.         IconFrame ike = new IconFrame();
  32.     }
  33. }
Jan 3 '10 #1
5 9082
pbrockway2
151 Recognized Expert New Member
The Java runtime has to be told where the file is!

Expand|Select|Wrap|Line Numbers
  1. ImageIcon loadIcon = new ImageIcon("load.gif");
  2.  
This will attempt to find the image in the "current working directory" - the directory from where the java executable was launched. Depending on where you place the image file it is not actually true that you must specify the full path.

Perhaps you could say what you are trying to do. Ie explain where the image files are placed with respect to the other files that make up the application.

A common approach is to place load.gif in the same directory as IconFrame.class or in the same directory as some other class file. Then you can use the Class methods getResource() or getResourceAsStream(). Notice that ImageIcon has a constructor that takes a URL argument which can be supplied by the getResource() method. This approach has the advantage that only relative paths need be used, and it will work when the application (.class and image files) are within a jar archive.
Jan 3 '10 #2
Scaran54
3 New Member
Hello,

I managed to fix the problem in the end by changing the PATH variable in system settings after quite alot of mild annoyance over something that I thought should have just worked.

Thanks very much for your response!
Jan 3 '10 #3
pbrockway2
151 Recognized Expert New Member
I'm glad you've got what you want.

However the operating system's (or shell's) PATH variable controls the directories within which it (the OS or shell) will look for executable files: something which has nothing to do with resource location within a Java (or any other) application.
Jan 4 '10 #4
Scaran54
3 New Member
This will attempt to find the image in the "current working directory" - the directory from where the java executable was launched. Depending on where you place the image file it is not actually true that you must specify the full path.
Before I changed the PATH file I had the images in the "current working directory" but it did not find them. I changed the PATH to the folder that holds my projects, is that correct?

Which class supplys the getResource() and getResourceAsStream() methods?

Thanks
Jan 4 '10 #5
pbrockway2
151 Recognized Expert New Member
@Scaran54
Honestly I can't see how the images would not be found if they were in the same directory as that from which the application was launched. Regardless of the setting of the OS PATH variable.

But perhaps I'm missing something. One way to tell would be to post a small example of what you mean: something that prints the cwd with System.out.println(System.getProperty("user.dir")) , reports the file's existence with new File("load.gif").exists(), creates the icon as you do now and then reports the value of loadIcon.getImage().

@Scaran54
They are in the Class class. (If you haven't used it before there is an index) page as part of the javadoc.)

You might use them like:

Expand|Select|Wrap|Line Numbers
  1. url loadURL = IconFrame.class.getResource("load.gif");
  2. ImageIcon imageIcon = new ImageIcon(loadURL);
  3.  
This code assumes that load.gif is placed in the same directory as IconFrame.class.

You might find the code in the How to Use Icons chapter of Sun's Tutorial useful: they describe how to load an icon, again from a location that is relative to the class doing the loading. (They use another Class method - getClass() - instead of using a class literal like ImageIcon.class.)
Jan 4 '10 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: David | last post by:
Hello, I'm starting with java and i'm having problems to display an image in a splitpanel with a scrollbar. I have different files but those required are described below. I think the problem is...
1
by: AIK | last post by:
Can someone show me or point me to a nice tutorial on showing imageicon images in a jframe? Thanks.
0
by: Mik | last post by:
I have a problem with drawing ImageIcon objects on a JPanel in a JApplet Everything is ok when I run applet locally, but when it's run from a server the images are not showing ImageIcon objects...
4
by: Vishnu | last post by:
I have a strange problem on WindowsXP proffessional with IE6 ,when i try to display a tiff file ,it is not showing ,small red x is comming up. I tried by dowloading latest IE from microsoft but...
2
by: c.anandkumar | last post by:
Hi All - I have some problems getting a small piece of javascript working correctly for Firefox. Here is what I am trying to do - 1. I have a form (like a search form) 2. I have many groups...
3
by: Brian Henry | last post by:
Is there anyway to prevent the white box that drops down when you do a combobox dropdown from showing? I am trying to make a custom combobox which requires showing a custom form in its place, but...
1
by: mario99 | last post by:
I have an application that contains buttons which contain icons and images. When I run the app in Eclipse, they show fine. However, when I create the JAR file and run it, no icon shows, and I don't...
1
crystal2005
by: crystal2005 | last post by:
Hi all, Can anyone tell me why my image doesn't appear in my frame's container? When I change imageIcon to just normal Label, it does go work. In another condition, if i don't use method...
2
by: Coreyja | last post by:
Im trying to display a png image by setting it as a ImageIcon and then putting that in a JLabel. I cant get it to display the image. I am using a null layout as it is the simplest way for me to get...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.