11 Replies - 12070 Views - Last Post: 18 April 2011 - 09:20 PM Rate Topic: -----

#1 Brewer  Icon User is offline

  • Awesome
  • member icon

Reputation: 179
  • View blog
  • Posts: 1,044
  • Joined: 14-June 10

Display an Image in a JFrame using ImageIcon

Posted 18 April 2011 - 06:05 PM

I've been trying to figure out how to display an image in a JFrame using ImageIcon, but I'm having a rough time. First off, I'd like to know whether this is the most effective way to do this. Secondly, what am I doing wrong?

I am using Ubuntu and I'm not sure that I am using the right filepath for the image.

import java.awt.Color;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.ImageIcon;
 
public class ProgrammingProject_2_2 {

	public static void main(String[] args) {

		Color myColor = new Color(150,250,150);
		
 		JFrame frame = new JFrame();
 		frame.setSize(400, 400);
 		JLabel label = new JLabel("Hello, World!");
 		ImageIcon image = new ImageIcon("home/james/Pictures/SheldonSmile.jpg");
 		JLabel imageLabel = new JLabel(image);
 		label.setOpaque(true);
 		label.setBackground(myColor);
 		frame.add(imageLabel);
 		frame.add(label);
 		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
 		frame.setVisible(true);

	}

}


Is This A Good Question/Topic? 0
  • +

Replies To: Display an Image in a JFrame using ImageIcon

#2 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8324
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Display an Image in a JFrame using ImageIcon

Posted 18 April 2011 - 06:27 PM

you mean that home/james/Pictures is a subdirectory of the directory where your .class is ?

Add
System.out.println(image);
after that line
ImageIcon image = new ImageIcon("home/james/Pictures/SheldonSmile.jpg");

If it prints: null you haven't read the image correctly
If it prints: SheldonSmile.jpg the image has been correctly read

Don't put a background color to your JLabel
Was This Post Helpful? 1
  • +
  • -

#3 EarthShaker  Icon User is offline

  • D.I.C Head

Reputation: 55
  • View blog
  • Posts: 186
  • Joined: 16-March 11

Re: Display an Image in a JFrame using ImageIcon

Posted 18 April 2011 - 06:29 PM

I believe you need to add a / in front of your home.
/home/james/etc.
Was This Post Helpful? 0
  • +
  • -

#4 JeremyC  Icon User is offline

  • D.I.C Head

Reputation: 6
  • View blog
  • Posts: 80
  • Joined: 12-September 10

Re: Display an Image in a JFrame using ImageIcon

Posted 18 April 2011 - 06:31 PM

I was having trouble with this last week and got help with it. The ImageIcon class was not fetching the image like it should so I was told to use this instead and it worked like a a charm.

ImageIcon image = new ImageIcon(getClass().getResource("path/to/image"));

This post has been edited by JeremyC: 18 April 2011 - 06:32 PM

Was This Post Helpful? 0
  • +
  • -

#5 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8324
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Display an Image in a JFrame using ImageIcon

Posted 18 April 2011 - 06:41 PM

required for an Applet not an application that has access to the file system.
Was This Post Helpful? 0
  • +
  • -

#6 Brewer  Icon User is offline

  • Awesome
  • member icon

Reputation: 179
  • View blog
  • Posts: 1,044
  • Joined: 14-June 10

Re: Display an Image in a JFrame using ImageIcon

Posted 18 April 2011 - 06:42 PM

@pbl /home/james/Pictures is the directory the image is in. Similar to C:\Users\James\Pictures on Windows 7.

Adding System.out.println(image); prints whatever is in the quotation marks (new ImageIcon("<prints whatever is here")). It doesn't seem to matter what is there, it will print anything and never prints Null.

@EarthShaker You're probably right, I've made this change.
Was This Post Helpful? 0
  • +
  • -

#7 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8324
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Display an Image in a JFrame using ImageIcon

Posted 18 April 2011 - 06:48 PM

View PostBrewer, on 18 April 2011 - 08:42 PM, said:

@pbl /home/james/Pictures is the directory the image is in. Similar to C:\Users\James\Pictures on Windows 7.

So should be
ImageIcon image = new ImageIcon("/home/james/Pictures/SheldonSmile.jpg");

Quote

Adding System.out.println(image); prints whatever is in the quotation marks

ImageIcon image = new ImageIcon("/home/james/Pictures/SheldonSmile.jpg");

Which quotation marks ? I do not see any in the following statement
System.out.println(image);
Was This Post Helpful? 0
  • +
  • -

#8 Brewer  Icon User is offline

  • Awesome
  • member icon

Reputation: 179
  • View blog
  • Posts: 1,044
  • Joined: 14-June 10

Re: Display an Image in a JFrame using ImageIcon

Posted 18 April 2011 - 07:14 PM

If I use the code
ImageIcon image = new ImageIcon("/home/james/Pictures/SheldonSmile.jpg");
then
System.out.println(image);
will print /home/james/Pictures/SheldonSmile.jpg

This post has been edited by Brewer: 18 April 2011 - 07:14 PM

Was This Post Helpful? 0
  • +
  • -

#9 EarthShaker  Icon User is offline

  • D.I.C Head

Reputation: 55
  • View blog
  • Posts: 186
  • Joined: 16-March 11

Re: Display an Image in a JFrame using ImageIcon

Posted 18 April 2011 - 07:17 PM

So, whats the problem now?
Was This Post Helpful? 0
  • +
  • -

#10 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8324
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Display an Image in a JFrame using ImageIcon

Posted 18 April 2011 - 07:25 PM

View PostBrewer, on 18 April 2011 - 09:14 PM, said:

If I use the code
ImageIcon image = new ImageIcon("/home/james/Pictures/SheldonSmile.jpg");
then
System.out.println(image);
will print /home/james/Pictures/SheldonSmile.jpg

So now we are sure that the .jpg file was read correctly and that image is a valid ImageIcon object.

So we can go fix the next problem which is ?

JFrame are BorderLayout by default so

frame.add(label);

will replace your imageLabel in the BorderLayout.CENTER region

do
// frame.add(label);

and it should work
Was This Post Helpful? 1
  • +
  • -

#11 Brewer  Icon User is offline

  • Awesome
  • member icon

Reputation: 179
  • View blog
  • Posts: 1,044
  • Joined: 14-June 10

Re: Display an Image in a JFrame using ImageIcon

Posted 18 April 2011 - 09:01 PM

That made the image show up. However, I'd like to see both the image and the text at the same time, preferably side-by-side. Do I need to choose a different layout?
Was This Post Helpful? 0
  • +
  • -

#12 macosxnerd101  Icon User is offline

  • Self-Trained Economist
  • member icon




Reputation: 10376
  • View blog
  • Posts: 38,415
  • Joined: 27-December 08

Re: Display an Image in a JFrame using ImageIcon

Posted 18 April 2011 - 09:20 PM

Add one, using BorderLayout.EAST as the second param in the add() method. For the other, use BorderLayout.WEST.
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1