javax.swing
Class ImageIcon

java.lang.Object sample code for java.lang.Object definition code for java.lang.Object 
  extended by javax.swing.ImageIcon
All Implemented Interfaces:
Serializable sample code for java.io.Serializable definition code for java.io.Serializable , Accessible sample code for javax.accessibility.Accessible definition code for javax.accessibility.Accessible , Icon sample code for javax.swing.Icon definition code for javax.swing.Icon

public class ImageIcon
extends Object sample code for java.lang.Object definition code for java.lang.Object
implements Icon sample code for javax.swing.Icon definition code for javax.swing.Icon , Serializable sample code for java.io.Serializable definition code for java.io.Serializable , Accessible sample code for javax.accessibility.Accessible definition code for javax.accessibility.Accessible

An implementation of the Icon interface that paints Icons from Images. Images that are created from a URL, filename or byte array are preloaded using MediaTracker to monitor the loaded state of the image.

For further information and examples of using image icons, see How to Use Icons in The Java Tutorial.

Warning: Serialized objects of this class will not be compatible with future Swing releases. The current serialization support is appropriate for short term storage or RMI between applications running the same version of Swing. As of 1.4, support for long term storage of all JavaBeansTM has been added to the java.beans package. Please see XMLEncoder sample code for java.beans.XMLEncoder definition code for java.beans.XMLEncoder .

See Also:
Serialized Form

Nested Class Summary
protected  class ImageIcon.AccessibleImageIcon sample code for javax.swing.ImageIcon.AccessibleImageIcon definition code for javax.swing.ImageIcon.AccessibleImageIcon
          This class implements accessibility support for the ImageIcon class.
 
Field Summary
protected static Component sample code for java.awt.Component definition code for java.awt.Component component sample code for javax.swing.ImageIcon.component definition code for javax.swing.ImageIcon.component
           
protected static MediaTracker sample code for java.awt.MediaTracker definition code for java.awt.MediaTracker tracker sample code for javax.swing.ImageIcon.tracker definition code for javax.swing.ImageIcon.tracker
           
 
Constructor Summary
ImageIcon sample code for javax.swing.ImageIcon.ImageIcon() definition code for javax.swing.ImageIcon.ImageIcon() ()
          Creates an uninitialized image icon.
ImageIcon sample code for javax.swing.ImageIcon.ImageIcon(byte[]) definition code for javax.swing.ImageIcon.ImageIcon(byte[]) (byte[] imageData)
          Creates an ImageIcon from an array of bytes which were read from an image file containing a supported image format, such as GIF, JPEG, or (as of 1.3) PNG.
ImageIcon sample code for javax.swing.ImageIcon.ImageIcon(byte[], java.lang.String) definition code for javax.swing.ImageIcon.ImageIcon(byte[], java.lang.String) (byte[] imageData, String sample code for java.lang.String definition code for java.lang.String  description)
          Creates an ImageIcon from an array of bytes which were read from an image file containing a supported image format, such as GIF, JPEG, or (as of 1.3) PNG.
ImageIcon sample code for javax.swing.ImageIcon.ImageIcon(java.awt.Image) definition code for javax.swing.ImageIcon.ImageIcon(java.awt.Image) (Image sample code for java.awt.Image definition code for java.awt.Image  image)
          Creates an ImageIcon from an image object.
ImageIcon sample code for javax.swing.ImageIcon.ImageIcon(java.awt.Image, java.lang.String) definition code for javax.swing.ImageIcon.ImageIcon(java.awt.Image, java.lang.String) (Image sample code for java.awt.Image definition code for java.awt.Image  image, String sample code for java.lang.String definition code for java.lang.String  description)
          Creates an ImageIcon from the image.
ImageIcon sample code for javax.swing.ImageIcon.ImageIcon(java.lang.String) definition code for javax.swing.ImageIcon.ImageIcon(java.lang.String) (String sample code for java.lang.String definition code for java.lang.String  filename)
          Creates an ImageIcon from the specified file.
ImageIcon sample code for javax.swing.ImageIcon.ImageIcon(java.lang.String, java.lang.String) definition code for javax.swing.ImageIcon.ImageIcon(java.lang.String, java.lang.String) (String sample code for java.lang.String definition code for java.lang.String  filename, String sample code for java.lang.String definition code for java.lang.String  description)
          Creates an ImageIcon from the specified file.
ImageIcon sample code for javax.swing.ImageIcon.ImageIcon(java.net.URL) definition code for javax.swing.ImageIcon.ImageIcon(java.net.URL) (URL sample code for java.net.URL definition code for java.net.URL  location)
          Creates an ImageIcon from the specified URL.
ImageIcon sample code for javax.swing.ImageIcon.ImageIcon(java.net.URL, java.lang.String) definition code for javax.swing.ImageIcon.ImageIcon(java.net.URL, java.lang.String) (URL sample code for java.net.URL definition code for java.net.URL  location, String sample code for java.lang.String definition code for java.lang.String  description)
          Creates an ImageIcon from the specified URL.
 
Method Summary
 AccessibleContext sample code for javax.accessibility.AccessibleContext definition code for javax.accessibility.AccessibleContext getAccessibleContext sample code for javax.swing.ImageIcon.getAccessibleContext() definition code for javax.swing.ImageIcon.getAccessibleContext() ()
          Gets the AccessibleContext associated with this ImageIcon.
 String sample code for java.lang.String definition code for java.lang.String getDescription sample code for javax.swing.ImageIcon.getDescription() definition code for javax.swing.ImageIcon.getDescription() ()
          Gets the description of the image.
 int getIconHeight sample code for javax.swing.ImageIcon.getIconHeight() definition code for javax.swing.ImageIcon.getIconHeight() ()
          Gets the height of the icon.
 int getIconWidth sample code for javax.swing.ImageIcon.getIconWidth() definition code for javax.swing.ImageIcon.getIconWidth() ()
          Gets the width of the icon.
 Image sample code for java.awt.Image definition code for java.awt.Image getImage sample code for javax.swing.ImageIcon.getImage() definition code for javax.swing.ImageIcon.getImage() ()
          Returns this icon's Image.
 int getImageLoadStatus sample code for javax.swing.ImageIcon.getImageLoadStatus() definition code for javax.swing.ImageIcon.getImageLoadStatus() ()
          Returns the status of the image loading operation.
 ImageObserver sample code for java.awt.image.ImageObserver definition code for java.awt.image.ImageObserver getImageObserver sample code for javax.swing.ImageIcon.getImageObserver() definition code for javax.swing.ImageIcon.getImageObserver() ()
          Returns the image observer for the image.
protected  void loadImage sample code for javax.swing.ImageIcon.loadImage(java.awt.Image) definition code for javax.swing.ImageIcon.loadImage(java.awt.Image) (Image sample code for java.awt.Image definition code for java.awt.Image  image)
          Loads the image, returning only when the image is loaded.
 void paintIcon sample code for javax.swing.ImageIcon.paintIcon(java.awt.Component, java.awt.Graphics, int, int) definition code for javax.swing.ImageIcon.paintIcon(java.awt.Component, java.awt.Graphics, int, int) (Component sample code for java.awt.Component definition code for java.awt.Component  c, Graphics sample code for java.awt.Graphics definition code for java.awt.Graphics  g, int x, int y)
          Paints the icon.
 void setDescription sample code for javax.swing.ImageIcon.setDescription(java.lang.String) definition code for javax.swing.ImageIcon.setDescription(java.lang.String) (String sample code for java.lang.String definition code for java.lang.String  description)
          Sets the description of the image.
 void setImage sample code for javax.swing.ImageIcon.setImage(java.awt.Image) definition code for javax.swing.ImageIcon.setImage(java.awt.Image) (Image sample code for java.awt.Image definition code for java.awt.Image  image)
          Sets the image displayed by this icon.
 void setImageObserver sample code for javax.swing.ImageIcon.setImageObserver(java.awt.image.ImageObserver) definition code for javax.swing.ImageIcon.setImageObserver(java.awt.image.ImageObserver) (ImageObserver sample code for java.awt.image.ImageObserver definition code for java.awt.image.ImageObserver  observer)
          Sets the image observer for the image.
 String sample code for java.lang.String definition code for java.lang.String toString sample code for javax.swing.ImageIcon.toString() definition code for javax.swing.ImageIcon.toString() ()
          Returns a string representation of this image.
 
Methods inherited from class java.lang.Object sample code for java.lang.Object definition code for java.lang.Object
clone sample code for java.lang.Object.clone() definition code for java.lang.Object.clone() , equals sample code for java.lang.Object.equals(java.lang.Object) definition code for java.lang.Object.equals(java.lang.Object) , finalize sample code for java.lang.Object.finalize() definition code for java.lang.Object.finalize() , getClass sample code for java.lang.Object.getClass() definition code for java.lang.Object.getClass() , hashCode sample code for java.lang.Object.hashCode() definition code for java.lang.Object.hashCode() , notify sample code for java.lang.Object.notify() definition code for java.lang.Object.notify() , notifyAll sample code for java.lang.Object.notifyAll() definition code for java.lang.Object.notifyAll() , wait sample code for java.lang.Object.wait() definition code for java.lang.Object.wait() , wait sample code for java.lang.Object.wait(long) definition code for java.lang.Object.wait(long) , wait sample code for java.lang.Object.wait(long, int) definition code for java.lang.Object.wait(long, int)
 

Field Detail

component sample code for javax.swing.ImageIcon.component

protected static final Component sample code for java.awt.Component definition code for java.awt.Component  component

tracker sample code for javax.swing.ImageIcon.tracker

protected static final MediaTracker sample code for java.awt.MediaTracker definition code for java.awt.MediaTracker  tracker
Constructor Detail

ImageIcon sample code for javax.swing.ImageIcon(java.lang.String, java.lang.String) definition code for javax.swing.ImageIcon(java.lang.String, java.lang.String)

public ImageIcon(String sample code for java.lang.String definition code for java.lang.String  filename,
                 String sample code for java.lang.String definition code for java.lang.String  description)
Creates an ImageIcon from the specified file. The image will be preloaded by using MediaTracker to monitor the loading state of the image.

Parameters:
filename - the name of the file containing the image
description - a brief textual description of the image
See Also:
ImageIcon(String) sample code for javax.swing.ImageIcon.ImageIcon(java.lang.String) definition code for javax.swing.ImageIcon.ImageIcon(java.lang.String)

ImageIcon sample code for javax.swing.ImageIcon(java.lang.String) definition code for javax.swing.ImageIcon(java.lang.String)

public ImageIcon(String sample code for java.lang.String definition code for java.lang.String  filename)
Creates an ImageIcon from the specified file. The image will be preloaded by using MediaTracker to monitor the loading state of the image. The specified String can be a file name or a file path. When specifying a path, use the Internet-standard forward-slash ("/") as a separator. (The string is converted to an URL, so the forward-slash works on all systems.) For example, specify:
    new ImageIcon("images/myImage.gif") 
The description is initialized to the filename string.

Parameters:
filename - a String specifying a filename or path
See Also:
getDescription() sample code for javax.swing.ImageIcon.getDescription() definition code for javax.swing.ImageIcon.getDescription()

ImageIcon sample code for javax.swing.ImageIcon(java.net.URL, java.lang.String) definition code for javax.swing.ImageIcon(java.net.URL, java.lang.String)

public ImageIcon(URL sample code for java.net.URL definition code for java.net.URL  location,
                 String sample code for java.lang.String definition code for java.lang.String  description)
Creates an ImageIcon from the specified URL. The image will be preloaded by using MediaTracker to monitor the loaded state of the image.

Parameters:
location - the URL for the image
description - a brief textual description of the image
See Also:
ImageIcon(String) sample code for javax.swing.ImageIcon.ImageIcon(java.lang.String) definition code for javax.swing.ImageIcon.ImageIcon(java.lang.String)

ImageIcon sample code for javax.swing.ImageIcon(java.net.URL) definition code for javax.swing.ImageIcon(java.net.URL)

public ImageIcon(URL sample code for java.net.URL definition code for java.net.URL  location)
Creates an ImageIcon from the specified URL. The image will be preloaded by using MediaTracker to monitor the loaded state of the image. The icon's description is initialized to be a string representation of the URL.

Parameters:
location - the URL for the image
See Also:
getDescription() sample code for javax.swing.ImageIcon.getDescription() definition code for javax.swing.ImageIcon.getDescription()

ImageIcon sample code for javax.swing.ImageIcon(java.awt.Image, java.lang.String) definition code for javax.swing.ImageIcon(java.awt.Image, java.lang.String)

public ImageIcon(Image sample code for java.awt.Image definition code for java.awt.Image  image,
                 String sample code for java.lang.String definition code for java.lang.String  description)
Creates an ImageIcon from the image.

Parameters:
image - the image
description - a brief textual description of the image

ImageIcon sample code for javax.swing.ImageIcon(java.awt.Image) definition code for javax.swing.ImageIcon(java.awt.Image)

public ImageIcon(Image sample code for java.awt.Image definition code for java.awt.Image  image)
Creates an ImageIcon from an image object. If the image has a "comment" property that is a string, then the string is used as the description of this icon.

Parameters:
image - the image
See Also:
getDescription() sample code for javax.swing.ImageIcon.getDescription() definition code for javax.swing.ImageIcon.getDescription() , Image.getProperty(java.lang.String, java.awt.image.ImageObserver) sample code for java.awt.Image.getProperty(java.lang.String, java.awt.image.ImageObserver) definition code for java.awt.Image.getProperty(java.lang.String, java.awt.image.ImageObserver)

ImageIcon sample code for javax.swing.ImageIcon(byte[], java.lang.String) definition code for javax.swing.ImageIcon(byte[], java.lang.String)

public ImageIcon(byte[] imageData,
                 String sample code for java.lang.String definition code for java.lang.String  description)
Creates an ImageIcon from an array of bytes which were read from an image file containing a supported image format, such as GIF, JPEG, or (as of 1.3) PNG. Normally this array is created by reading an image using Class.getResourceAsStream(), but the byte array may also be statically stored in a class.

Parameters:
imageData - an array of pixels in an image format supported by the AWT Toolkit, such as GIF, JPEG, or (as of 1.3) PNG
description - a brief textual description of the image
See Also:
Toolkit.createImage(java.lang.String) sample code for java.awt.Toolkit.createImage(java.lang.String) definition code for java.awt.Toolkit.createImage(java.lang.String)

ImageIcon sample code for javax.swing.ImageIcon(byte[]) definition code for javax.swing.ImageIcon(byte[])

public ImageIcon(byte[] imageData)
Creates an ImageIcon from an array of bytes which were read from an image file containing a supported image format, such as GIF, JPEG, or (as of 1.3) PNG. Normally this array is created by reading an image using Class.getResourceAsStream(), but the byte array may also be statically stored in a class. If the resulting image has a "comment" property that is a string, then the string is used as the description of this icon.

Parameters:
imageData - an array of pixels in an image format supported by the AWT Toolkit, such as GIF, JPEG, or (as of 1.3) PNG
See Also:
Toolkit.createImage(java.lang.String) sample code for java.awt.Toolkit.createImage(java.lang.String) definition code for java.awt.Toolkit.createImage(java.lang.String) , getDescription() sample code for javax.swing.ImageIcon.getDescription() definition code for javax.swing.ImageIcon.getDescription() , Image.getProperty(java.lang.String, java.awt.image.ImageObserver) sample code for java.awt.Image.getProperty(java.lang.String, java.awt.image.ImageObserver) definition code for java.awt.Image.getProperty(java.lang.String, java.awt.image.ImageObserver)

ImageIcon sample code for javax.swing.ImageIcon() definition code for javax.swing.ImageIcon()

public ImageIcon()
Creates an uninitialized image icon.

Method Detail

loadImage sample code for javax.swing.ImageIcon.loadImage(java.awt.Image) definition code for javax.swing.ImageIcon.loadImage(java.awt.Image)

protected void loadImage(Image sample code for java.awt.Image definition code for java.awt.Image  image)
Loads the image, returning only when the image is loaded.

Parameters:
image - the image

getImageLoadStatus sample code for javax.swing.ImageIcon.getImageLoadStatus() definition code for javax.swing.ImageIcon.getImageLoadStatus()

public int getImageLoadStatus()
Returns the status of the image loading operation.

Returns:
the loading status as defined by java.awt.MediaTracker
See Also:
MediaTracker.ABORTED sample code for java.awt.MediaTracker.ABORTED definition code for java.awt.MediaTracker.ABORTED , MediaTracker.ERRORED sample code for java.awt.MediaTracker.ERRORED definition code for java.awt.MediaTracker.ERRORED , MediaTracker.COMPLETE sample code for java.awt.MediaTracker.COMPLETE definition code for java.awt.MediaTracker.COMPLETE

getImage sample code for javax.swing.ImageIcon.getImage() definition code for javax.swing.ImageIcon.getImage()

public Image sample code for java.awt.Image definition code for java.awt.Image  getImage()
Returns this icon's Image.

Returns:
the Image object for this ImageIcon

setImage sample code for javax.swing.ImageIcon.setImage(java.awt.Image) definition code for javax.swing.ImageIcon.setImage(java.awt.Image)

public void setImage(Image sample code for java.awt.Image definition code for java.awt.Image  image)
Sets the image displayed by this icon.

Parameters:
image - the image

getDescription sample code for javax.swing.ImageIcon.getDescription() definition code for javax.swing.ImageIcon.getDescription()

public String sample code for java.lang.String definition code for java.lang.String  getDescription()
Gets the description of the image. This is meant to be a brief textual description of the object. For example, it might be presented to a blind user to give an indication of the purpose of the image. The description may be null.

Returns:
a brief textual description of the image

setDescription sample code for javax.swing.ImageIcon.setDescription(java.lang.String) definition code for javax.swing.ImageIcon.setDescription(java.lang.String)

public void setDescription(String sample code for java.lang.String definition code for java.lang.String  description)
Sets the description of the image. This is meant to be a brief textual description of the object. For example, it might be presented to a blind user to give an indication of the purpose of the image.

Parameters:
description - a brief textual description of the image

paintIcon sample code for javax.swing.ImageIcon.paintIcon(java.awt.Component, java.awt.Graphics, int, int) definition code for javax.swing.ImageIcon.paintIcon(java.awt.Component, java.awt.Graphics, int, int)

public void paintIcon(Component sample code for java.awt.Component definition code for java.awt.Component  c,
                      Graphics sample code for java.awt.Graphics definition code for java.awt.Graphics  g,
                      int x,
                      int y)
Paints the icon. The top-left corner of the icon is drawn at the point (x, y) in the coordinate space of the graphics context g. If this icon has no image observer, this method uses the c component as the observer.

Specified by:
paintIcon sample code for javax.swing.Icon.paintIcon(java.awt.Component, java.awt.Graphics, int, int) definition code for javax.swing.Icon.paintIcon(java.awt.Component, java.awt.Graphics, int, int) in interface Icon sample code for javax.swing.Icon definition code for javax.swing.Icon
Parameters:
c - the component to be used as the observer if this icon has no image observer
g - the graphics context
x - the X coordinate of the icon's top-left corner
y - the Y coordinate of the icon's top-left corner

getIconWidth sample code for javax.swing.ImageIcon.getIconWidth() definition code for javax.swing.ImageIcon.getIconWidth()

public int getIconWidth()
Gets the width of the icon.

Specified by:
getIconWidth sample code for javax.swing.Icon.getIconWidth() definition code for javax.swing.Icon.getIconWidth() in interface Icon sample code for javax.swing.Icon definition code for javax.swing.Icon
Returns:
the width in pixels of this icon

getIconHeight sample code for javax.swing.ImageIcon.getIconHeight() definition code for javax.swing.ImageIcon.getIconHeight()

public int getIconHeight()
Gets the height of the icon.

Specified by:
getIconHeight sample code for javax.swing.Icon.getIconHeight() definition code for javax.swing.Icon.getIconHeight() in interface Icon sample code for javax.swing.Icon definition code for javax.swing.Icon
Returns:
the height in pixels of this icon

setImageObserver sample code for javax.swing.ImageIcon.setImageObserver(java.awt.image.ImageObserver) definition code for javax.swing.ImageIcon.setImageObserver(java.awt.image.ImageObserver)

public void setImageObserver(ImageObserver sample code for java.awt.image.ImageObserver definition code for java.awt.image.ImageObserver  observer)
Sets the image observer for the image. Set this property if the ImageIcon contains an animated GIF, so the observer is notified to update its display. For example:
     icon = new ImageIcon(...)
     button.setIcon(icon);
     icon.setImageObserver(button);
 

Parameters:
observer - the image observer

getImageObserver sample code for javax.swing.ImageIcon.getImageObserver() definition code for javax.swing.ImageIcon.getImageObserver()

public ImageObserver sample code for java.awt.image.ImageObserver definition code for java.awt.image.ImageObserver  getImageObserver()
Returns the image observer for the image.

Returns:
the image observer, which may be null

toString sample code for javax.swing.ImageIcon.toString() definition code for javax.swing.ImageIcon.toString()

public String sample code for java.lang.String definition code for java.lang.String  toString()
Returns a string representation of this image.

Overrides:
toString sample code for java.lang.Object.toString() definition code for java.lang.Object.toString() in class Object sample code for java.lang.Object definition code for java.lang.Object
Returns:
a string representing this image

getAccessibleContext sample code for javax.swing.ImageIcon.getAccessibleContext() definition code for javax.swing.ImageIcon.getAccessibleContext()

public AccessibleContext sample code for javax.accessibility.AccessibleContext definition code for javax.accessibility.AccessibleContext  getAccessibleContext()
Gets the AccessibleContext associated with this ImageIcon. For image icons, the AccessibleContext takes the form of an AccessibleImageIcon. A new AccessibleImageIcon instance is created if necessary.

Specified by:
getAccessibleContext sample code for javax.accessibility.Accessible.getAccessibleContext() definition code for javax.accessibility.Accessible.getAccessibleContext() in interface Accessible sample code for javax.accessibility.Accessible definition code for javax.accessibility.Accessible
Returns:
an AccessibleImageIcon that serves as the AccessibleContext of this ImageIcon