Results 1 to 2 of 2
  1. #1
    saz25 is offline Member
    Join Date
    Dec 2007
    Location
    Nashua, NH
    Posts
    1
    Rep Power
    0

    Default Icon initialization problem

    Hi,
    I am using Netbeans 5.0 to write my Java GUI app.
    I have a strange problem. In my java application, I have a JLabel that I'm using as an icon. Actually, I have several of these. A default icon is supposed to appear when the app starts up. When the user clicks on certain buttons (and things happen), the code changes the icon to a different gif file. Here is the code I use to initialize one of them:

    sdConfig8.setIcon(new javax.swing.ImageIcon("C:\\filepath\\mycangui\\off .gif"));

    When I run my app on my own PC (the one I used to build the app), the icons are all initialized correctly. When I run the app on another PC, these icons are not initialized. So the icon doesn't appear. When the user clicks on the specific button to generate a specific event, the code execution changes the icon to the correct gif. This works fine on all machines.

    The issue is why can't I get the icons to initialize on machines other than my development PC?

    Thanks in advance,
    Steve

  2. #2
    tim's Avatar
    tim
    tim is offline Senior Member
    Join Date
    Dec 2007
    Posts
    435
    Rep Power
    8

    Default Directory problem.

    Your problem lies here:
    Java Code:
    sdConfig8.setIcon(new javax.swing.ImageIcon("C:\\filepath\\mycangui\\off .gif"));
    Firstly, not all directories are described by the backslash operator. To make your directory references independent of the operating system that you are running on, use File.seperator. Secondly, the image will not be loaded because it does not automatically exist on the other machine's hard drive at that location. (unless you put it there) It is safer to use file names that are relative to the directory of your application. For example:

    Java Code:
    ImageIcon icon = new ImageIcon("data" + File.seperator + "off .gif");
    I assume that you intended the space in "off .gif".

    Inside your application's directory folder, you could have another folder called "data" with the image in there.
    ;)
    Eyes dwelling into the past are blind to what lies in the future. Step carefully.

Similar Threads

  1. Initialization and re-assignment of arrays
    By Java Tip in forum java.lang
    Replies: 0
    Last Post: 04-14-2008, 09:38 PM
  2. initialization value problem
    By ravian in forum New To Java
    Replies: 2
    Last Post: 01-28-2008, 11:54 AM
  3. How to set an Icon in a Label?
    By Soda in forum New To Java
    Replies: 2
    Last Post: 12-07-2007, 01:38 PM
  4. Lazy Initialization
    By onegcr in forum New To Java
    Replies: 1
    Last Post: 08-14-2007, 04:29 PM
  5. log4j initialization
    By arfatkhan in forum Web Frameworks
    Replies: 3
    Last Post: 08-10-2007, 08:42 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •