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?!?
:
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.
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?!?
:
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
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
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.
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).
javapenguin (July 8th, 2010)
Tried that. Now what goes before the / ?
Does it matter by chance how many bytes the image has?
Do I put the path name in the quotes or the entire location of where the picture is?
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.
javapenguin (July 8th, 2010)
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
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.
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:
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).
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.
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.
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:
- Right click the Project and choose Import.
- Select the file(s) of choice
- This imports the files into the 'root' location of your project.
- 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.
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.