473,288 Members | 2,725 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,288 software developers and data experts.

ImageIcon not showing

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 9074
pbrockway2
151 Expert 100+
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
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 Expert 100+
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
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 Expert 100+
@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...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...

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.