Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 15 of 15

Thread: Stupid thing can't find image icon right in front of it!!!

  1. #1
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Angry Stupid thing can't find image icon right in front of it!!!

    I did ImageIcon like that API suggested, but it's not finding the file, while clearly exists as I was able to import it to the java projects itself, yet it can't find it. What's going on?!?

    :


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Stupid thing can't find image icon right in front of it!!!

    It is very difficult to help you without any code and information. Are you specifying an absolute or relative path? Are you trying to open this from a class that is in a package? The more info the better

  3. #3
    Member
    Join Date
    Jul 2010
    Location
    Washington, USA
    Posts
    307
    Thanks
    16
    Thanked 43 Times in 39 Posts

    Default Re: Stupid thing can't find image icon right in front of it!!!

    Is the Image in the same scope as your program? If it is you can use the name directly. else you have to use c:blahh blahh

  4. #4
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default Re: Stupid thing can't find image icon right in front of it!!!

    I had it imported into the project, yes.

    I tried lots of things, including having the word images/ in the quotes.

    It's showing in the thing that shows all the components of the package in Eclipse.

    It's under a column called "Referenced Libraries".

    Also, by any chance is the image being lost, as I have a JComboBox, that will show certain pictures when certain choices are selected. I have the JComboBox outside of the JSplitPane but still in the JFrame, and I have a JTextArea in one of the sides and it is supposed to have a label that is set to an icon on the other. When another option is selected, the previous picture is removed from the component, though it should be able to come back if selected again in the JComboBox, and a new one takes its place. This is done to keep from getting a build up off too many pictures that I don't want showing. This is kind of like a column in an online newspaper, not I'm not making an applet, where the story and the image change inside the JSplitPane when the user selects a different story in the JComboBox. That's kind of what it's like. Except the JLabel, set to the image, won't show, as the method that, very cleverly, will make a new ImageIcon from a file and also a description, which I pass it in the parameters. However, it is returning path not found. "image name".

    I got the createImageIcon method from this: http://java.sun.com/docs/books/tutor...LabelDemo.java

    Do I have to have the whole path name from the description in order for it to recognize the image if it isn't in the same scope as the program?

    By description, I don't mean the parameter, I mean the thing that tells me where the file is and all that stuff when I click on the image and click "Properties" on the popup menu that comes when I click on it on my computer?
    Last edited by javapenguin; July 8th, 2010 at 03:54 PM.

  5. #5
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Stupid thing can't find image icon right in front of it!!!

    The path to the image depends upon where your image in in your Eclipse project. A matter or preference, but I usually either import it into the project by right clicking the project and going to import and from there choose the image, or for larger projects and to keep things a bit more ordered create a new package called images, right click that and import the image there. From your createImageIcon function, call the 'full' path: either "/myImage.jpg" or "/images/myImage.jpg" - note the beginning slash specifying the root folder of the project (otherwise the JVM will look in the location where the calling class is located).

  6. The Following User Says Thank You to copeg For This Useful Post:

    javapenguin (July 8th, 2010)

  7. #6
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default Re: Stupid thing can't find image icon right in front of it!!!

    Tried that. Now what goes before the / ?

  8. #7
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default Re: Stupid thing can't find image icon right in front of it!!!

    Does it matter by chance how many bytes the image has?

  9. #8
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default Re: Stupid thing can't find image icon right in front of it!!!

    Do I put the path name in the quotes or the entire location of where the picture is?

  10. #9
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,676
    Thanks
    25
    Thanked 329 Times in 305 Posts

    Default Re: Stupid thing can't find image icon right in front of it!!!

    try something. Type in the absolute path when you create the file and see if it can see it then. Remember that if your using Windows, file directories are moved through by "\\" in java, not "/".

    If it can find the file at that point, it means that something in your declaration of the relative path.

  11. The Following User Says Thank You to aussiemcgr For This Useful Post:

    javapenguin (July 8th, 2010)

  12. #10
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Question Re: Stupid thing can't find image icon right in front of it!!!

    What's an absolute path?

    Is it the entire long drawn out path with the folder C and everything

    Ok, is the absolute path the file location? I have no clue what an absolute path is.

    Ok, here are several examples of things I'm going to try.

    Option One: "//images//myIcon.jpg"

    Option Two: "//myIcon.jpg""

    Option Three: "//projectName//myIcon.jpg"

    Option Four: "\\projectName\\src\\subFolder\\Users\\me\\Downloa ds\\myIcon.jpg"

    Option Five: Option four without the beginning \\

    Option Six: Option Two without \\ at beginning
    Last edited by javapenguin; July 8th, 2010 at 06:01 PM. Reason: Clarifying

  13. #11
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,895
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: Stupid thing can't find image icon right in front of it!!!

    The absolute or relative paths can be navigated either using \\ or / in windows. The only reason you need \\ is because \ by itself signals the beginning of an escape character. The actual path you will get will only have one \ because you "escaped" out of the escape character.

    System.out.println("C:\\"); // should print "C:\"
    System.out.println("C:/"); // should print out "C:/"

    An absolute path is the path that completely describes the location of the file starting from which partition it's in (say, C:\ in windows). A relative path is a path that describes the exact same location but relative from some point.

    You can test to see if you're even finding the image:

    Fiile theImageFile = new File("C:\image.png"); // obviously, your relative OR absolute path should replace the one I put
    if(theImageFile.exists())
    {
         System.out.println("I found the file!");
    }
    else
    {
         System.out.println("I didn't find the file :(");
    }

    In eclipse, the startup folder is the base project folder. If you put your image right into the base project folder, you can just put the file name and it should find it (fyi, this is a relative path).

    File theImageFile = new File("image.png"); // creates a file reference to image.png in the eclipse project startup folder

    If your image is not located in exactly the same folder you're referencing from, you need to use "dots" to navigate upwards, then specifically spell out which folders to navigate back down.

    File file1 = new File("../image.png"); // the image is in the folder above this one
    File file2 = new File("../../Other Folder/image.png"); // the image is two folders up, then inside Other Folder
    File file3 = new File("./image.png"); // image is in the same folder. Notice only one dot
    File file4 = new File("image.png"); // same thing as file3
    Last edited by helloworld922; July 9th, 2010 at 01:07 AM.

  14. #12
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,676
    Thanks
    25
    Thanked 329 Times in 305 Posts

    Default Re: Stupid thing can't find image icon right in front of it!!!

    Quote Originally Posted by javapenguin View Post
    What's an absolute path?

    Is it the entire long drawn out path with the folder C and everything

    Ok, is the absolute path the file location? I have no clue what an absolute path is.

    Ok, here are several examples of things I'm going to try.

    Option One: "//images//myIcon.jpg"

    Option Two: "//myIcon.jpg""

    Option Three: "//projectName//myIcon.jpg"

    Option Four: "\\projectName\\src\\subFolder\\Users\\me\\Downloa ds\\myIcon.jpg"

    Option Five: Option four without the beginning \\

    Option Six: Option Two without \\ at beginning
    The absolute path is the entire path. One easy way of obtaining this without doing any real work yourself is this.

    import javax.swing.JFileChooser;
    import java.io.File;
     
    public class FileOpen {
     
        public static void main(String args[]) throws Exception
        {
        	JFileChooser fileChooser = new JFileChooser();
        	fileChooser.showOpenDialog(null);
    	    File file = fileChooser.getSelectedFile();
    	    System.out.println(file.getAbsolutePath());
        }
    }

    This will open up a basic file chooser. Go through the file chooser and select the file and press ok. It will then print out the absolute path of the file you selected. I use the absolute path file returns when I need to reference the xls files I read and write to using SmartXLS, so it does work to connect to a file.

    Insistently, I guess you dont have to use the \\ to go through directories. However I've used it since I was told I did and it has always worked for me, so I dunno.

  15. #13
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Stupid thing can't find image icon right in front of it!!!

    To clarify this a bit, absolute and relative paths have a somewhat different meaning when discussing a computer file system versus the package system the original poster is using (eg using Class.getResource()). Using the createImageIcon method (which uses Class.getResource()) a relative path is relative to the class, and the absolute path is relative to the package.

    To reiterate my instructions I alluded to above for Eclipse:
    1. Right click the Project and choose Import.
    2. Select the file(s) of choice
    3. This imports the files into the 'root' location of your project.
    4. To access this image, for example say its name is "myImage.jpg", use the package absolute path: createImageIcon("/myImage.jpg", "Image description");
      • If you imported your image into the package the calling class resides in, remove the beginning slash
      • You can import an image into any package, but you must tell the JVM where the image is by using unix type paths ('../' to backtrack up a directory, '/' at the beginning to specify the root, './' to specify this directory, etc...)
    Last edited by copeg; July 8th, 2010 at 08:57 PM.

  16. #14
    Member
    Join Date
    Jul 2010
    Location
    Washington, USA
    Posts
    307
    Thanks
    16
    Thanked 43 Times in 39 Posts

    Default Re: Stupid thing can't find image icon right in front of it!!!

    In java you can use forward slash, but you have to use 2 of them. EXAMPLE: "SRC//Images//icon.png"

  17. #15
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,895
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: Stupid thing can't find image icon right in front of it!!!

    Quote Originally Posted by Brt93yoda View Post
    In java you can use forward slash, but you have to use 2 of them. EXAMPLE: "SRC//Images//icon.png"
    This is incorrect, see my above post on why you don't need two forward slashes. Do note that using 2 of them doesn't cause any problems, though (actually, any number of them doesn't matter, so long as you have at least one).

    // assuming you have a file named test.txt at C:/temp/test.txt
    File file = new File("C:/temp/test.txt");
    if(file.exists())
    {
         System.out.println("single slash exists");
    }
    file = new File("C://temp//test.txt");
    if(file.exists())
    {
         System.out.println("double slash exists");
    }
    file = new File("C:////temp////test.txt");
    if(file.exists())
    {
         System.out.println("quadruple slash exists");
    }

    By the same token, the same applies to back-slash, just remember that back-slash is the escape character so they MUST come in pairs of 2.

    // assuming you have a file named test.txt at C:/temp/test.txt
    File file = new File("C:\\temp\\test.txt");
    if(file.exists())
    {
         System.out.println("single slash exists");
    }
    file = new File("C:\\\\temp\\\\test.txt");
    if(file.exists())
    {
         System.out.println("double slash exists");
    }
    file = new File("C:\\\\\\\\temp\\\\\\\\test.txt");
    if(file.exists())
    {
         System.out.println("quadruple slash exists");
    }
    Last edited by helloworld922; July 9th, 2010 at 01:06 AM.

Similar Threads

  1. Trying 3 different ways to do the same thing, each one is wrong
    By shemer77 in forum What's Wrong With My Code?
    Replies: 14
    Last Post: June 4th, 2010, 07:01 PM
  2. [SOLVED] I cant load the icon!!!!
    By chronoz13 in forum AWT / Java Swing
    Replies: 12
    Last Post: January 22nd, 2010, 03:52 AM
  3. Pixel Alteration in an image/image rotation
    By bguy in forum Java Theory & Questions
    Replies: 3
    Last Post: November 1st, 2009, 10:50 AM
  4. Icon change and lib folder problem
    By LeonLanford in forum What's Wrong With My Code?
    Replies: 0
    Last Post: October 21st, 2009, 03:00 AM
  5. My doubt may be stupid
    By kalees in forum Object Oriented Programming
    Replies: 2
    Last Post: September 22nd, 2009, 02:38 AM