-1

I looked all over the place but I am still stuck on how directory works to find the image to put onto the JPanel. Where is the image supposed to be? I clicked on properties for my image and it shows Location: C:\Users\Joseph\Pictures\Background and the picture's name is random.jpg.

I am trying to add an image to a tab using tabbedPane. Here is what I have so far, and I am not able to do it.

JPanel flPanel = new JPanel();

flPanel.setLayout(new FlowLayout());

ImageIcon image = new ImageIcon(getClass().getResource("")); 
// Tried /Users/Joseph/Pictures/Background/random.jpg and doesn't work

JLabel j1 = new JLabel(image);

flPanel.add(j1);

tabbedPane.add("Tab 2", flPanel);

Is the picture supposed to be in the same package file as the project? Or is it supposed to be in the source file to be able to just do "random.jpg"?

Theodoros Chatzigiannakis
  • 28,773
  • 8
  • 68
  • 104
user3404854
  • 1
  • 2
  • 6
  • 2
    Read the section from the Swing tutorial on [How to Use Icons](http://docs.oracle.com/javase/tutorial/uiswing/components/icon.html) for a working example. – camickr Sep 24 '14 at 03:10
  • 1
    Please don't vandalize your own question, once it has been answered. – Theodoros Chatzigiannakis Sep 24 '14 at 07:12
  • 1
    @TheodorosChatzigiannakis: FYI, he tried to vandalize a question again with [this question from today](http://stackoverflow.com/questions/26210236/why-is-my-fillpolygon-shape-looking-odd-java-gui). I've notified the site moderators. We will have to watch him. – Hovercraft Full Of Eels Oct 06 '14 at 22:35

2 Answers2

2

If you want the image to be available to your application at runtime, then you should consider making sure that the image is included within your Jar when you application is built.

From the sounds of it, you are using Netbeans, you should copy the image to a directory within your src directory of your project.

You should then be able to use...

BufferedImage bi = ImageIO.read(getClass().getResource("/full/path/to/image/random.jpg"));
ImageIcon image = new ImageIcon(bi);

The path to the image should be the full path (from the context of the src directory) within your project.

That is, if you placed the image in the resources directory within the src directory, then you would use /resources/random.jpg as the path/file name

Take a look at Reading/Loading an Image for more details

MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
  • I am actually using eclipse. Would it work the same way on eclipse? – user3404854 Sep 24 '14 at 05:41
  • The location of the file within Eclipse is different. Rather than been stored in the `src` directory they need to be stored in a resources directory within the project (same location as `src`) and included within the build process. The process of looking up the resource is the same from a runtime perspective... – MadProgrammer Sep 24 '14 at 05:43
  • I can't seem to locate the resources directly. What is it labeled as? I see src, bin, .settings files, .classpath, and .project. – user3404854 Sep 24 '14 at 06:03
  • You need to create it and add include it as part of your class-path and build process.. – MadProgrammer Sep 24 '14 at 06:04
  • How do I create it? Do I do it on eclipse? Please if could you guide me step by step... been stuck on this part for days trying to figure out. Thank you – user3404854 Sep 24 '14 at 06:10
  • Unfortunately, I don't use eclipse, but as I understand it you should be able to create a directory/folder within the project from within eclipse – MadProgrammer Sep 24 '14 at 06:15
  • Something like http://stackoverflow.com/questions/2458305/adding-folder-to-eclipse-classpath and http://stackoverflow.com/questions/12266761/eclipse-adding-all-the-jars-from-a-folder-in-java-classpath and http://stackoverflow.com/questions/18768861/how-to-place-source-directory-in-build-path-in-eclipse might help – MadProgrammer Sep 24 '14 at 06:16
  • No problem. I see two candidates, Source file and just Folder. Which one could it be? And after creating it, could I just drag the image into the file shown in eclipse? – user3404854 Sep 24 '14 at 06:18
  • I'd suggest folder is what you want and yes, I you should be able to drag the image into the folder – MadProgrammer Sep 24 '14 at 06:19
  • then afterwards, I should replace the line ImageIcon image = new ImageIcon(getClass().getResource("")); with BufferedImage bi = ImageIO.read(new File("random.jpg")) ImageIcon image = new ImageIcon(bi); and it should work? – user3404854 Sep 24 '14 at 06:24
  • No, if you've included the "resources" directory within the classpath/buildpath, you should be able to `getClass().getResource("...")`, as the image will become an embedded resource within your application Jar... – MadProgrammer Sep 24 '14 at 06:27
  • Are you referring to the Folder as the "resources" directory? – user3404854 Sep 24 '14 at 06:31
  • As, technically, it can be called what ever you like, but convention usually has it named as "resources". The contents will be added to the classpath and build path (if you've configured it), which means `getClass().getResource(...)` will be able to find the files contained within it... – MadProgrammer Sep 24 '14 at 06:38
  • So creating the Folder and dragging the image into the folder isn't considered as being added to the classpath and build path? Gosh, never thought making an image on Jpanel would be so hard... – user3404854 Sep 24 '14 at 06:44
  • Yep, now you know why I don't use eclipse ;) – MadProgrammer Sep 24 '14 at 06:56
1

getClass().getResource(...) will only get resources inside the classpath.

You can use ImageIO.read(File) like this:

BufferedImage bi = ImageIO.read(new File("C:\\Users\\Joseph\\Pictures\\Background\random.jpg"))
ImageIcon image = new ImageIcon(bi);
DiogoSantana
  • 2,404
  • 2
  • 19
  • 24