How to put an image on JButton?

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

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 works, an error occurs: Exception in thread "main" java.lang.IllegalArgumentException: input == null! This error occurs on the line:

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

I do not think that this error is due to the fact that the file was not found in the java code, since the My Images folder is located in the src folder (I use Eclipse), as recommended by the link above.
Does anyone have any ideas what might be the problem?
Thanks.

+4
source share
5 answers

When using Eclipse, you do not keep your images in the src folder, instead you create a Source Folder for this purpose. See this link for adding images to the resources folder in Eclipse .

+9
source

Use this to create a button.

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

And what you do, set the Image icon as the icon. This does not work because the setIcon() method requires objects that implement the Icon interface. Hope this helps.

+2
source

Try marking the line before the package name in getResource() as follows:

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

You can simply 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 is in a JAR, I usually create an extraction function for me so that I can reuse 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 will do

 JButton jb = new JButton(retrieveIcon("/pic.png")); 
+1
source
 Image img = ImageIO.read(getClass().getResource("Images\\BBishopB.gif")); 

This line tries to do too much in one go, which makes it difficult to track the error when it is received. 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 NPE. Even if this does not tell you why you are getting an error message, this greatly complicates the problem.

+1
source

Source: https://habr.com/ru/post/1437392/


All Articles