How to put an image on a JButton?

23,396

Solution 1

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.

Solution 2

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.

Solution 3

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

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

Solution 4

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"));

Solution 5

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.

Share:
23,396

Related videos on Youtube

Tristan Hull
Author by

Tristan Hull

Updated on December 05, 2020

Comments

  • Tristan Hull
    Tristan Hull over 3 years

    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.

    • vstm
      vstm over 11 years
      Have you actually checked the return value of getResource()?
  • David Kroukamp
    David Kroukamp over 11 years
    +1 nice link :) yup I think it might be that he doesnt have the foward slash
  • nIcE cOw
    nIcE cOw over 11 years
    More so the concern for me, is not using the Source FOlder for the said thingy, as the OP said the images are in the src Folder, which I doubt is the right way IMHO
  • davidXYZ
    davidXYZ over 11 years
    Resource Folders are mere conventions. You don't have to use a special 'resource folder' for your images to be read. Your path just has to be correct.
  • nIcE cOw
    nIcE cOw over 11 years
    It sure does, when you get to the task of running your program, whatever is inside your Resource Folder automatically goes to the bin folder. So when you create your .jar file, using Eclipse, you don't have to worry about, whether the path specified will work or not.
  • Tristan Hull
    Tristan Hull over 11 years
    Thanks very much I definitely try this.
  • Andrew Thompson
    Andrew Thompson over 11 years
    That answer was good until the shouting at the end. Now I have a head-ache.. :(