3

I try to display an image using a JLabel. This is my project navigator: enter image description here

From SettingsDialog.java I want to display an image using following code:

        String path = "/images/sidebar-icon-48.png";
        File file = new File(path);
        Image image;
        try {
            image = ImageIO.read(file);

            JLabel label = new JLabel(new ImageIcon(image));
            header.add(label); // header is a JPanel
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

The code throws an exception: Can't read input file!

Is the path of the image is wrong?

Paul Samsotha
  • 205,037
  • 37
  • 486
  • 720
MaTTP
  • 953
  • 2
  • 12
  • 27
  • The path to the file is incorrect. The exception is pretty clear. – Pete B. Feb 18 '14 at 14:25
  • 2
    Please have a look at [this answer](http://stackoverflow.com/a/9866659/1057230) of mine for __Eclipse__ related part, might be this will shed some more light. This link is also available at info of [tag:embedded-resource] – nIcE cOw Feb 18 '14 at 14:33

3 Answers3

5

Don't read from a file, read from the class path

image = ImageIO.read(getClass().getResource(path));
-or-
image = ImageIO.read(MyClass.class.getResource(path));

When you use a File object, you're telling the program to read from the file system, which will make your path invalid. The path you are using is correct though, when reading from the class path, as you should be doing.

See the wiki on embedded resource. Also see getResource()


UPDATE Test Run

enter image description here

package org.apache.openoffice.sidebar;

import javax.swing.*;

public class SomeClass {
    public SomeClass() {
        ImageIcon icon = new ImageIcon(
              SomeClass.class.getResource("/images/sidebar-icon-48.png"));
        JLabel label = new JLabel(icon);

        JFrame frame = new JFrame("Test");
        frame.add(label);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.pack();
        frame.setLocationByPlatform(true);
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable(){
            public void run() {
                new SomeClass();
            }
        });
    }
}
Community
  • 1
  • 1
Paul Samsotha
  • 205,037
  • 37
  • 486
  • 720
  • Thanks for your answer, but the code doesn't work. If I try to print getClass().getResource(path) result I see "NULL" – MaTTP Feb 18 '14 at 14:49
  • If this `"/images/sidebar-icon-48.png"` is the file path you are using, and the above image is your actual file structure, it _should_ work. I've never had a problem with it. – Paul Samsotha Feb 18 '14 at 14:51
  • Are you sure image is inside jar / copied to output folder/ bin? – tgkprog Feb 18 '14 at 15:07
  • No, how do I check? I need to configure it? – MaTTP Feb 18 '14 at 15:17
  • @MaTTP After you build your project, the image should get copied into the class path. If you don't have your build automatically create a jar, just check in the `bin`, it should get built into the directory. Look for the `images` dir n there. – Paul Samsotha Feb 18 '14 at 15:19
  • I'don't have a bin class in the export file. – MaTTP Feb 18 '14 at 15:23
  • It's not a class. It's a directory. check your `workspace/projectroot` there should be a `bin` in there. I'm not sure how to open the file structure in eclipse. Just check in your file system (not eclipse package explorer) – Paul Samsotha Feb 18 '14 at 15:27
  • @MattP See if [**this**](http://help.eclipse.org/indigo/index.jsp?topic=%2Forg.eclipse.jdt.doc.user%2Ftasks%2Ftasks-33.htm) helps. – Paul Samsotha Feb 18 '14 at 15:40
  • Solved!! The JAR doesn't contains image folder. Thank you to all! – MaTTP Feb 18 '14 at 22:22
1

"/images/sidebar-icon-48.png" is root path. On windows would be c:\images\sidebar-icon-48.png or d:\images\sidebar-icon-48.png depending on current drive (java converts the / to \ - not an issue). Linux images would a child of root /images/sidebar-icon-48.png Need to load relative to class or relative to the jar that had the class (if you do not want to store images inside jar.

In big projects its nice to have images and other resources outside the jar so the jar is smaller and more importantly its easy to change the resources without fiddling with jars/ wars.

Since you seem to be making a add on for open office, you will have to keep everything in jar and so peeskillet answer is right. But make sure your images folder is being packed in the jar. Extract the jar ising the jar command or rename the file to zip and extract.

Or check and fix project settings. How to make a jar in eclipse ... latest one has a wizard that makes an ant script or this SO

Community
  • 1
  • 1
tgkprog
  • 4,493
  • 4
  • 41
  • 70
1

try to use this directly :

    JLabel label = new JLabel(new ImageIcon(path));

and delete these line :

File file = new File(path);

image = ImageIO.read(file);

if error still exist paste the following error