Question

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?

Was it helpful?

Solution

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();
            }
        });
    }
}

OTHER TIPS

"/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

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

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top