Tristan Hull
Tristan Hull

Reputation: 360

How to put an image on a JButton?

I am writing a program that requires I have a button with an image over top of it, however, so far, I have not been able to get it to work. I have checked several other posts on this site, including How do I add an image to a JButton.
My Code:

public class Tester extends JFrame
{
    public Tester()
    {
        JPanel panel = new JPanel();
        getContentPane().add(panel);
        panel.setLayout(null);

        setTitle("Image Test");
        setSize(300,300);
        setLocationRelativeTo(null);
        setDefaultCloseOperation(EXIT_ON_CLOSE);

        JButton button = new JButton();
        try 
        {
            Image img = ImageIO.read(getClass().getResource("Images\\BBishopB.gif"));
            button.setIcon(new ImageIcon(img));
        } 
        catch (IOException ex) {}

        button.setBounds(100,100,100,100);
        panel.add(button);
    }

    public static void main(String[] args)
    {
        Tester test = new Tester();
        test.setVisible(true);
    }
}

When this code runs, an error results: Exception in thread "main" java.lang.IllegalArgumentException: input == null! This error occurs at the line:

Image img = ImageIO.read(getClass().getResource("Images\\BBishopB.gif"));

I don't think that this error is due to the file not being found by java code, as my Images folder is in the src folder (I am using Eclipse) as recommended by the above link.
Does anyone have any ideas to what the problem may be?
Thanks.

Upvotes: 4

Views: 24733

Answers (5)

Code-Apprentice
Code-Apprentice

Reputation: 83527

Image img = ImageIO.read(getClass().getResource("Images\\BBishopB.gif"));

This line tries to do too much all at one time which makes it difficult to track down an error when you get one. I suggest splitting it up:

URL imgURL = getClass().getResource("Images\\BBishopB.gif");
Image img = ImageIO.read(imgURL);

Now you can use the Eclipse debugger to check the return value of imgURL, which is the most likely candidate for the NPE. Even though this doesn't tell you why you get an error message, it narrows down the problem considerably.

Upvotes: 1

nIcE cOw
nIcE cOw

Reputation: 24626

While using Eclipse, you don't keep your images into src folder instead you create a Source Folder for this purpose. Please refer to this link regarding how to add images to resource folder in Eclipse.

Upvotes: 9

davidXYZ
davidXYZ

Reputation: 729

You can just find the image directly:

JButton jb = new JButton(new ImageIcon("pic.png")); //pic is in project root folder
//Tip: After placing the image in project folder, refresh the project in Eclipse.

OR if the image will be in a JAR, I usually create a function to do the retrieval for me so that I can re-use it.

public static ImageIcon retrieveIcon(String path){
    java.net.URL imgUrl = 'classpackage'.'classname'.class.getResource(path);
    ImageIcon icon = new ImageIcon(imgUrl);
    return icon;
}

Then I'll do,

JButton jb = new JButton(retrieveIcon("/pic.png"));

Upvotes: 1

David Kroukamp
David Kroukamp

Reputation: 36423

Try putting a foward slash before the package name in getResource() like so:

Image img = ImageIO.read(getClass().getResource("/Images/BBishopB.gif"));

Upvotes: 2

Sri Harsha Chilakapati
Sri Harsha Chilakapati

Reputation: 11950

Use this to create the button.

JButton button = new JButton(new ImageIcon(getClass().getClassLoader()
                                          .getResource("Images/BBishopB.gif")));

And what you are doing is setting the Image as the icon. This doesn't work because the setIcon() method requires objects which implement the Icon interface. Hope this helps.

Upvotes: 2

Related Questions