Hello, I've looked all over for the solution to this problem, but they all present a similar solution to this one and they all don't work. I've created the JFrame in the constructor for the class that this method is contained in.

public void print()
	{
		JPanel panel = new JPanel();
		panel.setBackground(Color.GREEN);
		ImageIcon icon = new ImageIcon("image.jpg");
		if(icon !=null)
			JOptionPane.showMessageDialog(null,"there is a value");
		else
			JOptionPane.showMessageDialog(null,"fail");
		JLabel label = new JLabel();
		label.setIcon(icon);
			
		panel.add(label);
		frame.getContentPane().add(panel);
		frame.setVisible(true);
	}

Any help would be appreciated

Recommended Answers

All 5 Replies

Please explain what your problem is.
Copy and paste the full text of the error messages here.

Hello, I've looked all over for the solution to this problem, but they all present a similar solution to this one and they all don't work.

Well, actually, they do, which is why you see it described so often. It's not the solution that doesn't work, it's your implementation of it.
So a little less arrogance and a bit more code (and a proper explanation of your problem) would help us fix your program

While the background will display, the image will not, and I'm confused as to why it will not.

The code works for me. Does the message display correctly? ("there is a value")
Can you post an image of the gui that you see.
Also is the image file at the correct path. ("image.jpg") . Have you tried the full path?
Also have you tried setting the size of the frame? frame.setSize(400, 400); Also just because the icon is not null, doesn't mean that the image file exists. Whatever you put as file, the instance would be created and it would be not null even if the image does not exist

Oh thanks javaAddict, putting the full file location worked, thanks 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.