Charlie
Charlie

Reputation: 3374

Need help setting the background image using JLabel?

I just started GUI programming in Java and I am having trouble setting the background image to a JFrame using JLabel. I have read many answers to the same question on this website but the code is too complicated for a beginner.

My source code is as follows and the image I'm using is already in src folder (I get the output window but there is no image in it):

public class staffGUI extends JFrame {
private JLabel imageLabel = new JLabel(new ImageIcon("staff-directory.jpg"));

private JPanel bxPanel = new JPanel();

public staffGUI(){
    super("Staff Management");

    bxPanel.setLayout(new GridLayout(1,1));
    bxPanel.add(imageLabel);

    this.setLayout(new GridLayout(1,1));
    this.add(bxPanel);

    this.setVisible(true);
    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    this.setLocationRelativeTo(null);
    this.setResizable(false);
    this.pack();
}

Upvotes: 0

Views: 463

Answers (2)

MadProgrammer
MadProgrammer

Reputation: 347244

ImageIcon(String) "Creates an ImageIcon from the specified file". The actual physical image is loaded in a background thread, so even though the call might return immediately, the actually loading could still be running in the background.

This means that ImageIcon does not throw any errors if the image can't be loaded, making sometimes annoying to work with. I prefer to use ImageIO.read where possible, as it will throw an IOException when it can't read a image for some reason.

The reason you image is not loading is because the image doesn't actually exist from the context of the JVM, which is looking in the current working directory of the image.

When you included resources within the context of the program, they can no longer be addressed as files and need to be loaded through the use of Class#getResource or Class#getResourceAsStream, depending on your needs.

For example

imageLabel = new JLabel(getClass().getResource("/staffdirectory/staff-directory.jpg"));

Where possible, you should supply the path to the image from the context of the source root

Can you give an example how I can use the "ImageIO.read" in my code?

import java.awt.GridLayout;
import java.awt.image.BufferedImage;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

public class staffGUI extends JFrame {

    private JLabel imageLabel;

    private JPanel bxPanel = new JPanel();

    public staffGUI() {
        super("Staff Management");

        imageLabel = new JLabel();
        try {
            BufferedImage img = ImageIO.read(getClass().getResource("/staffdirectory/staff-directory.jpg"));
            imageLabel.setIcon(new ImageIcon(img));
        } catch (IOException ex) {
            ex.printStackTrace();
        }

        bxPanel.setLayout(new GridLayout(1, 1));
        bxPanel.add(imageLabel);

        this.setLayout(new GridLayout(1, 1));
        this.add(bxPanel);

        this.setVisible(true);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setLocationRelativeTo(null);
        this.setResizable(false);
        this.pack();

    }
}

Upvotes: 1

raghavanm
raghavanm

Reputation: 13

Try this example i am sure it will work. Make sure that you have a images folder inside your src folder and put the image in it.

private JLabel imageLabel = new JLabel(getClass().getResource("/staffdirectory/staff-directory.jpg")); 

will not work and eclipse will give error as JLabel(URL) is not defined; secondly private modifier is not allowed here.

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.Toolkit;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;

class BackgroundImageJFrame extends JFrame {

    private static final long serialVersionUID = 6337428393053702097L;
    JButton b1;
    JLabel l1;

    public BackgroundImageJFrame() {
        setTitle("Background Color for JFrame");
        setSize(400, 400);
        setLocationRelativeTo(null);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setVisible(true);

        setLayout(new BorderLayout());

        JLabel background = new JLabel(
                new ImageIcon(Toolkit.getDefaultToolkit().getImage(
                        getClass().getResource("/images/free-wallpaper-4.jpg"))));
        add(background);
        background.setLayout(new FlowLayout());
        l1 = new JLabel("Here is a button");
        b1 = new JButton("I am a button");
        background.add(l1);
        background.add(b1);

        setSize(400, 400);
    }

    public static void main(String args[]) {
        new BackgroundImageJFrame();
    }
}

Upvotes: 0

Related Questions