I'm trying to add an image to the top of my GUI as a logo.
I tried to put it in a JPanel then add the JPanel at the top of the GUI, the only problem is that my image isnt going into my JPanel.
Heres my code for it

image = new ImageIcon("reboot_logo.jpeg");
JPanel imagePanel = new JPanel();
imagePanel.setLayout(new BorderLayout());
JLabel icon = new JLabel();
icon.setIcon(image);
imagePanel.add(icon, BorderLayout.NORTH);
Container cp = getContentPane();
cp.setLayout(new GridLayout(3, 1));
cp.add(imagePanel);

Any ideas why this wont work?

Thanks

Recommended Answers

All 9 Replies

try this

 icon.setIconImage(image);

I tried, it didn't work.........................

Post a small complete program that compiles, executes and shows the problem.

new ImageIcon doesn't throw an exception if it can't find the image file, so it's a good idea to try just printing image to see if it's got credible width./height etc

Ok here goes.
Program

package sample;



import java.awt.*;
import javax.swing.*;
/**
 *
 * @author trunx
 */
public class samplebody extends JFrame
{
    JPanel imagePanel;
    ImageIcon image;

    public samplebody()
    {
        imagePanel = new JPanel();
        imagePanel.setBackground(Color.WHITE);
        image = new ImageIcon("reboot_logo.jpeg");//I THINK THE PROBLEM IS HERE
        JLabel icon = new JLabel();
        icon.setIcon(image);
        //add items to image panel
        imagePanel.add(icon);

        //add panels to container
        Container cp = getContentPane();
        cp.add(imagePanel);


        // set gui
        setVisible(true);
        setSize(500, 300);
        setResizable(false);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setTitle("Reboot Booking In Form");


    }
}

Main method

package sample;


public class Sample {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) 
    {
        samplebody app = new samplebody();
    }
}

Thanks for looking.................

You can check to see exactly where it's looking for the file by
System.out.println(new File("reboot_logo.jpeg").getCanonicalPath());

Quoted Text Here

You can check to see exactly where it's looking for the file by
System.out.println(new File("reboot_logo.jpeg").getCanonicalPath());

I just done this, and the image is in the folder its looking in for it! Would there be any other reason it won't show?

Thanks

The posted code works for me (after I changed the image file path to one on my PC).
image = new ImageIcon("images/duke.gif");

I feel very silly now, spelling mistake I just couldn't see!

Thank you so much for your help..............

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.