954,536 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Display an image using java GUI

I'm trying to get a GUI application to display an image. So far I've got this bit of code:

import javax.swing.*;
import java.awt.*;

public class DemoImage extends JFrame {
	public void showImage() {
		
				
		// creates the actual frame with title 'My GUI' and dimensions
		JFrame frame = new JFrame("My GUI");
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.setSize(200, 50);
		frame.setResizable(false);
		frame.setLocationRelativeTo(null);
		
		// Inserts the image icon
		String imgStr = "image/pic1";
			
			
		ImageIcon image = new ImageIcon(imgStr);
		JLabel label1 = new JLabel(" ", image, JLabel.CENTER);
		frame.getContentPane().add(label1);
		
		frame.validate();
		frame.setVisible(true);
		
		
		
	}
	public static void main(String[] args) {
		DemoImage show1 = new DemoImage();
		show1.showImage();
	}

}


When i run it all i get is a window with a title "MY GUI". What else should i add so the image is actually displayed?

Thanks in advance

ymf
Newbie Poster
15 posts since May 2008
Reputation Points: 10
Solved Threads: 0
 

I don't know for sure by try the full path of the image with the extension

javaAddict
Nearly a Senior Poster
Team Colleague
3,329 posts since Dec 2007
Reputation Points: 1,014
Solved Threads: 448
 

This is the problem

String imgStr = "image/pic1";


, you did not provide file extension like pic1.jpeg, pic1.png, pic1.gif

peter_budo
Code tags enforcer
Moderator
15,436 posts since Dec 2004
Reputation Points: 2,806
Solved Threads: 902
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You