jedcute29
jedcute29

Reputation: 13

How can I set the icon of jlabel as background image using a jbutton?

I'm using Jframe form for this program and here's the button code I've tried. When I ran the program and clicked the button nothing happens. Pls help.

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                         
    ImageIcon icondir = new ImageIcon("C:\\Users\\Awesome\\Desktop\\myaJbQq.jpg");
    ID_Background.setIcon(icondir);
}                       

Upvotes: 0

Views: 58

Answers (2)

Gorbles
Gorbles

Reputation: 1168

ID_Background.repaint();

Should fix your issue. If not, I'm going to need more code than what you've provided (what the ID_Background is, how your GUI is setup, etc).

Upvotes: 0

Cristian Sulea
Cristian Sulea

Reputation: 264

This file (image) C:\\Users\\Awesome\\Desktop\\myaJbQq.jpg does not exists.

The problem with ImageIcon is that is not throwing any exception if the file/resource is missing. Try to load the icon this way:

ImageIcon icondir = new ImageIcon(
    ImageIO.read(
        new File("C:\\Users\\Awesome\\Desktop\\myaJbQq.jpg")));

You will have for sure an exception:

Exception in thread "main" javax.imageio.IIOException: Can't read input file!
    at javax.imageio.ImageIO.read(ImageIO.java:1301)
    ...

Upvotes: 1

Related Questions