I wanted to test having a program with a simple png image on it. I wrote a short program that does this, but I can't seem to get the path right. I have checked, checked again, rechecked, and quadruple checked my path name as to not get it right, but this image will not display, no matter what I do. I used a short class wrote by Oracle in the ImageIcon documentation (the creaetImageIcon()
) to accomplish this, but it doesn't seem to help. I'll post the entire program below, as it is very short.
package practiceImages;
import java.awt.BorderLayout;
import java.awt.Toolkit;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
public class ImageIconGUITest {
public static void main(String[] args) {
ImageIconGUITest gui = new ImageIconGUITest();
gui.display();
}
private ImageIcon createImageIcon(String path, String description) {
java.net.URL imgURL = getClass().getResource(path);
if (imgURL != null) {
return new ImageIcon(imgURL, description);
} else {
System.err.println("Couldn't find file: " + path);
return null;
}
}
private void display() {
JFrame frame = new JFrame();
JLabel label = new JLabel(createImageIcon(
"Users/Evan/javaItems/Sprites_and_Other_Art/green.png", "the color green"));
frame.add(BorderLayout.CENTER, label);
frame.setSize(500, 500);
frame.setVisible(true);
}
}
You need to do
C:\\Test\\test.png
and notC:/Test/test.png
Using this fixed it for me:
To get the path of a image to a text filed, this code will help you
//txtPath is the text filed use todiplay the path //lblImage is the label which shows the image
The
getResource(String)
method will only find resources that are on the run-time class-path of the application. Since this image seems like an application resource (i.e. supplied by you as part of the application) it should be put on the run-time class-path.E.G. Most IDEs have a place you can put resources within the project structure, that will automatically be included at run-time. Move (or copy) the image to that path.
Then it becomes a matter of providing the correct
String
. Let us imagine your project is set up something like this:So
Application.java
is inpackage com.our;
, while the image is in the pathresources/green.png
.If accessing the image from the
Application
, the correct path would be (drum roll please..)"/resources/green.png"
Notes
/
is important. It tells the JRE we want to look for the image from the 'root of the class-path', as opposed to using a path relative to the package of the class itself."/resources/green.png"
will not locate an image named"/resources/Green.png"
or"/resources/green.PNG"
.Eclipse paths
src
directory, selectProperties
at the bottom of the menu.Location
.bin
directory that contains classes and (hopefully) the image.use double slash instead of one , i had this problem and i fixed it . ill show you an example :
public Driver (){
Firstly, you've supplied a relative path, so the system is looking for the image relative to the location you executed the program.
Secondly, the path should have a drive spec or at least a leading
/
. Depending on your setup, something like 'C:/Users/Evan/javaItems/Sprites_and_Other_Art/green.png' should work (you may need to change the drive spec to meet your system)Thirdly, make sure that the file exists in the specified location,
System.out.println(new File("C:/Users/Evan/javaItems/Sprites_and_Other_Art/green.png").exists())
should returntrue
, other wise the file is in the wrong location.A relative path basically means a path location relative to the programs execution. So, if you were running the program from
C:/Program Files/MyAwesomeApplication
for example, a relative path ofUsers/Evan/javaItems/Sprites_and_Other_Art/green.png
would become an absolute path ofC:/Program Files/MyAwesomeApplication/Users/Evan/javaItems/Sprites_and_Other_Art/green.png
. This describes the path from the root location to the file/folder in question.You can test this by using
System.out.println(new File("C:/Users/Evan/javaItems/Sprites_and_Other_Art/green.png").getAbsolutePath())
which will give you the full path.