Top Description Inners Fields Constructors Methods
javax.swing

public Class ImageIcon

extends Object
implements Icon, Serializable, Accessible
Class Inheritance
All Implemented Interfaces
javax.accessibility.Accessible, java.io.Serializable, javax.swing.Icon
Known Direct Subclasses
javax.swing.plaf.synth.SynthParser.LazyImageIcon, sun.swing.ImageIconUIResource
Annotations
@SuppressWarnings:serial
Imports
java.awt.Component, .Graphics, .IllegalComponentStateException, .Image, .MediaTracker, .Toolkit, java.awt.image.ColorModel, .ImageObserver, .MemoryImageSource, .PixelGrabber, java.beans.BeanProperty, .ConstructorProperties, .Transient, java.io.IOException, .ObjectInputStream, .ObjectOutputStream, .Serial, .Serializable, java.net.URL, java.security.AccessControlContext, .AccessController, .PrivilegedAction, .ProtectionDomain, java.util.Locale, javax.accessibility.Accessible, .AccessibleContext, .AccessibleIcon, .AccessibleRole, .AccessibleState, .AccessibleStateSet, sun.awt.AWTAccessor, .AppContext

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 JavaBeans has been added to the java.beans package. Please see java.beans.XMLEncoder.

Authors
Jeff Dinkins, Lynn Monsanto
Since
1.2

Nested and Inner Type Summary

Modifier and TypeClass and Description
protected class
ImageIcon.AccessibleImageIcon

This class implements accessibility support for the ImageIcon class.

Field Summary

Modifier and TypeField and Description
private ImageIcon.AccessibleImageIcon
accessibleContext

--- Accessibility Support ---

protected static final Component
component

Deprecated since 1.8
Do not use this shared component, which is used to track image loading.
pack-priv String
private transient String
pack-priv int
pack-priv transient Image
pack-priv ImageObserver
pack-priv transient int
private transient URL
private static int
mediaTrackerID

Id used in loading images from MediaTracker.

protected static final MediaTracker
tracker

Deprecated since 1.8
Do not use this shared media tracker, which is used to load images.
private static final Object
pack-priv int

Constructor Summary

AccessConstructor and Description
public
ImageIcon(String
the name of the file containing the image
filename
,
String
a brief textual description of the image
description
)

Creates an ImageIcon from the specified file.

public
ImageIcon(String
a String specifying a filename or path
filename
)

Creates an ImageIcon from the specified file.

public
ImageIcon(URL
the URL for the image
location
,
String
a brief textual description of the image
description
)

Creates an ImageIcon from the specified URL.

public
ImageIcon(URL
the URL for the image
location
)

Creates an ImageIcon from the specified URL.

public
ImageIcon(Image
the image
image
,
String
a brief textual description of the image
description
)

Creates an ImageIcon from the image.

public
ImageIcon(Image
the image
image
)

Creates an ImageIcon from an image object.

public
ImageIcon(byte[]
an array of pixels in an image format supported by the AWT Toolkit, such as GIF, JPEG, or (as of 1.3) PNG
imageData
,
String
a brief textual description of the image
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.

public
ImageIcon(byte[]
an array of pixels in an image format supported by the AWT Toolkit, such as GIF, JPEG, or (as of 1.3) PNG
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.

public
ImageIcon()

Creates an uninitialized image icon.

Method Summary

Modifier and TypeMethod and Description
private static Component
public AccessibleContext

Returns:

an AccessibleImageIcon that serves as the AccessibleContext of this ImageIcon
getAccessibleContext
()

Implements javax.accessibility.Accessible.getAccessibleContext.

Gets the AccessibleContext associated with this ImageIcon.
public String

Returns:

a brief textual description of the image
getDescription
()

Gets the description of the image.

public int

Returns:

the height in pixels of this icon
getIconHeight
()

Implements javax.swing.Icon.getIconHeight.

Gets the height of the icon.
public int

Returns:

the width in pixels of this icon
getIconWidth
()

Implements javax.swing.Icon.getIconWidth.

Gets the width of the icon.
public Image

Returns:

the Image object for this ImageIcon
getImage
()

Returns this icon's Image.

public int

Returns:

the loading status as defined by java.awt.MediaTracker
getImageLoadStatus
()

Returns the status of the image loading operation.

public ImageObserver

Returns:

the image observer, which may be null
getImageObserver
()

Returns the image observer for the image.

private int
getNextID()

Returns an ID to use with the MediaTracker in loading an image.

private MediaTracker
getTracker()

Returns the MediaTracker for the current AppContext, creating a new MediaTracker if necessary.

protected void
loadImage(Image
the image
image
)

Loads the image, returning only when the image is loaded.

public synchronized void
paintIcon(Component
the component to be used as the observer if this icon has no image observer
c
,
Graphics
the graphics context
g
,
int
the X coordinate of the icon's top-left corner
x
,
int
the Y coordinate of the icon's top-left corner
y
)

Implements javax.swing.Icon.paintIcon.

Paints the icon.
private void
public void
setDescription(String
a brief textual description of the image
description
)

Sets the description of the image.

public void
setImage(Image
the image
image
)

Sets the image displayed by this icon.

public void
setImageObserver(ImageObserver
the image observer
observer
)

Sets the image observer for the image.

public String

Returns:

a string representing this image
toString
()

Overrides java.lang.Object.toString.

Returns a string representation of this image.
private void
Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAllwaitwaitwait

Field Detail

accessibleContextback to summary
private ImageIcon.AccessibleImageIcon accessibleContext

--- Accessibility Support ---

componentback to summary
protected static final Component component

Deprecated

since 1.8

Do not use this shared component, which is used to track image loading. It is left for backward compatibility only.

Annotations
@SuppressWarnings:removal
@Deprecated
descriptionback to summary
pack-priv String description
filenameback to summary
private transient String filename
heightback to summary
pack-priv int height
imageback to summary
pack-priv transient Image image
imageObserverback to summary
pack-priv ImageObserver imageObserver
loadStatusback to summary
pack-priv transient int loadStatus
locationback to summary
private transient URL location
mediaTrackerIDback to summary
private static int mediaTrackerID

Id used in loading images from MediaTracker.

trackerback to summary
protected static final MediaTracker tracker

Deprecated

since 1.8

Do not use this shared media tracker, which is used to load images. It is left for backward compatibility only.

Annotations
@Deprecated
TRACKER_KEYback to summary
private static final Object TRACKER_KEY
widthback to summary
pack-priv int width

Constructor Detail

ImageIconback to summary
public ImageIcon(String filename, 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:String

the name of the file containing the image

description:String

a brief textual description of the image

See Also
ImageIcon(String)
ImageIconback to summary
public ImageIcon(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:String

a String specifying a filename or path

Annotations
@ConstructorProperties:description
See Also
getDescription
ImageIconback to summary
public ImageIcon(URL location, 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:URL

the URL for the image

description:String

a brief textual description of the image

See Also
ImageIcon(String)
ImageIconback to summary
public ImageIcon(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:URL

the URL for the image

See Also
getDescription
ImageIconback to summary
public ImageIcon(Image image, String description)

Creates an ImageIcon from the image.

Parameters
image:Image

the image

description:String

a brief textual description of the image

ImageIconback to summary
public ImageIcon(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:Image

the image

See Also
getDescription, java.awt.Image#getProperty
ImageIconback to summary
public ImageIcon(byte[] imageData, 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:byte[]

an array of pixels in an image format supported by the AWT Toolkit, such as GIF, JPEG, or (as of 1.3) PNG

description:String

a brief textual description of the image

See Also
java.awt.Toolkit#createImage
ImageIconback to summary
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:byte[]

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
java.awt.Toolkit#createImage, getDescription, java.awt.Image#getProperty
ImageIconback to summary
public ImageIcon()

Creates an uninitialized image icon.

Method Detail

createNoPermsComponentback to summary
private static Component createNoPermsComponent()
Annotations
@SuppressWarnings:removal
getAccessibleContextback to summary
public AccessibleContext getAccessibleContext()

Implements javax.accessibility.Accessible.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.

Returns:AccessibleContext

an AccessibleImageIcon that serves as the AccessibleContext of this ImageIcon

Annotations
@BeanProperty
expert:true
description:The AccessibleContext associated with this ImageIcon.
Since
1.3
getDescriptionback to summary
public 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:String

a brief textual description of the image

getIconHeightback to summary
public int getIconHeight()

Implements javax.swing.Icon.getIconHeight.

Gets the height of the icon.

Returns:int

the height in pixels of this icon

getIconWidthback to summary
public int getIconWidth()

Implements javax.swing.Icon.getIconWidth.

Gets the width of the icon.

Returns:int

the width in pixels of this icon

getImageback to summary
public Image getImage()

Returns this icon's Image.

Returns:Image

the Image object for this ImageIcon

Annotations
@Transient
getImageLoadStatusback to summary
public int getImageLoadStatus()

Returns the status of the image loading operation.

Returns:int

the loading status as defined by java.awt.MediaTracker

See Also
java.awt.MediaTracker#ABORTED, java.awt.MediaTracker#ERRORED, java.awt.MediaTracker#COMPLETE
getImageObserverback to summary
public ImageObserver getImageObserver()

Returns the image observer for the image.

Returns:ImageObserver

the image observer, which may be null

Annotations
@Transient
getNextIDback to summary
private int getNextID()

Returns an ID to use with the MediaTracker in loading an image.

getTrackerback to summary
private MediaTracker getTracker()

Returns the MediaTracker for the current AppContext, creating a new MediaTracker if necessary.

loadImageback to summary
protected void loadImage(Image image)

Loads the image, returning only when the image is loaded.

Parameters
image:Image

the image

paintIconback to summary
public synchronized void paintIcon(Component c, Graphics g, int x, int y)

Implements javax.swing.Icon.paintIcon.

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.

Parameters
c:Component

the component to be used as the observer if this icon has no image observer

g:Graphics

the graphics context

x:int

the X coordinate of the icon's top-left corner

y:int

the Y coordinate of the icon's top-left corner

readObjectback to summary
private void readObject(ObjectInputStream s) throws ClassNotFoundException, IOException
Annotations
@Serial
setDescriptionback to summary
public void setDescription(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:String

a brief textual description of the image

setImageback to summary
public void setImage(Image image)

Sets the image displayed by this icon.

Parameters
image:Image

the image

setImageObserverback to summary
public void setImageObserver(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:ImageObserver

the image observer

toStringback to summary
public String toString()

Overrides java.lang.Object.toString.

Returns a string representation of this image.

Returns:String

a string representing this image

writeObjectback to summary
private void writeObject(ObjectOutputStream s) throws IOException
Annotations
@Serial
javax.swing back to summary

protected Class ImageIcon.AccessibleImageIcon

extends AccessibleContext
implements AccessibleIcon, Serializable
Class Inheritance
All Implemented Interfaces
java.io.Serializable, javax.accessibility.AccessibleIcon
Annotations
@SuppressWarnings:serial

This class implements accessibility support for the ImageIcon class. It provides an implementation of the Java Accessibility API appropriate to image icon user-interface elements.

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 JavaBeans has been added to the java.beans package. Please see java.beans.XMLEncoder.

Since
1.3

Field Summary

Inherited from javax.accessibility.AccessibleContext:
ACCESSIBLE_ACTION_PROPERTYACCESSIBLE_ACTIVE_DESCENDANT_PROPERTYACCESSIBLE_CARET_PROPERTYACCESSIBLE_CHILD_PROPERTYACCESSIBLE_COMPONENT_BOUNDS_CHANGEDACCESSIBLE_DESCRIPTION_PROPERTYACCESSIBLE_HYPERTEXT_OFFSETACCESSIBLE_INVALIDATE_CHILDRENACCESSIBLE_NAME_PROPERTYACCESSIBLE_SELECTION_PROPERTYACCESSIBLE_STATE_PROPERTYACCESSIBLE_TABLE_CAPTION_CHANGEDACCESSIBLE_TABLE_COLUMN_DESCRIPTION_CHANGEDACCESSIBLE_TABLE_COLUMN_HEADER_CHANGEDACCESSIBLE_TABLE_MODEL_CHANGEDACCESSIBLE_TABLE_ROW_DESCRIPTION_CHANGEDACCESSIBLE_TABLE_ROW_HEADER_CHANGEDACCESSIBLE_TABLE_SUMMARY_CHANGEDACCESSIBLE_TEXT_ATTRIBUTES_CHANGEDACCESSIBLE_TEXT_PROPERTYACCESSIBLE_VALUE_PROPERTYACCESSIBLE_VISIBLE_DATA_PROPERTYaccessibleDescriptionaccessibleNameaccessibleParent

Constructor Summary

AccessConstructor and Description
protected
AccessibleImageIcon()

Constructs an AccessibleImageIcon.

Method Summary

Modifier and TypeMethod and Description
public Accessible

Returns:

the nth Accessible child of the object
getAccessibleChild
(int
zero-based index of child
i
)

Implements abstract javax.accessibility.AccessibleContext.getAccessibleChild.

Returns the nth Accessible child of the object.
public int

Returns:

the number of accessible children in the object.
getAccessibleChildrenCount
()

Implements abstract javax.accessibility.AccessibleContext.getAccessibleChildrenCount.

Returns the number of accessible children in the object.
public String

Returns:

the description of the icon
getAccessibleIconDescription
()

Implements javax.accessibility.AccessibleIcon.getAccessibleIconDescription.

Gets the description of the icon.
public int

Returns:

the height of the icon
getAccessibleIconHeight
()

Implements javax.accessibility.AccessibleIcon.getAccessibleIconHeight.

Gets the height of the icon.
public int

Returns:

the width of the icon
getAccessibleIconWidth
()

Implements javax.accessibility.AccessibleIcon.getAccessibleIconWidth.

Gets the width of the icon.
public int

Returns:

the index of this object in its parent; -1 if this object does not have an accessible parent.
getAccessibleIndexInParent
()

Implements abstract javax.accessibility.AccessibleContext.getAccessibleIndexInParent.

Gets the index of this object in its accessible parent.
public Accessible

Returns:

the Accessible parent of this object -- can be null if this object does not have an Accessible parent
getAccessibleParent
()

Overrides javax.accessibility.AccessibleContext.getAccessibleParent.

Gets the Accessible parent of this object.
public AccessibleRole

Returns:

an instance of AccessibleRole describing the role of the object
getAccessibleRole
()

Implements abstract javax.accessibility.AccessibleContext.getAccessibleRole.

Gets the role of this object.
public AccessibleStateSet

Returns:

an instance of AccessibleStateSet containing the current state set of the object
getAccessibleStateSet
()

Implements abstract javax.accessibility.AccessibleContext.getAccessibleStateSet.

Gets the state of this object.
public Locale

Returns:

the locale of this object
getLocale
()

Implements abstract javax.accessibility.AccessibleContext.getLocale.

Returns the locale of this object.
private void
public void
setAccessibleIconDescription(String
the description of the icon
description
)

Implements javax.accessibility.AccessibleIcon.setAccessibleIconDescription.

Sets the description of the icon.
private void
Inherited from javax.accessibility.AccessibleContext:
addPropertyChangeListenerfirePropertyChangegetAccessibleActiongetAccessibleComponentgetAccessibleDescriptiongetAccessibleEditableTextgetAccessibleIcongetAccessibleNamegetAccessibleRelationSetgetAccessibleSelectiongetAccessibleTablegetAccessibleTextgetAccessibleValueremovePropertyChangeListenersetAccessibleDescriptionsetAccessibleNamesetAccessibleParent

Constructor Detail

AccessibleImageIconback to summary
protected AccessibleImageIcon()

Constructs an AccessibleImageIcon.

Method Detail

getAccessibleChildback to summary
public Accessible getAccessibleChild(int i)

Implements abstract javax.accessibility.AccessibleContext.getAccessibleChild.

Returns the nth Accessible child of the object.

Parameters
i:int

zero-based index of child

Returns:Accessible

the nth Accessible child of the object

getAccessibleChildrenCountback to summary
public int getAccessibleChildrenCount()

Implements abstract javax.accessibility.AccessibleContext.getAccessibleChildrenCount.

Returns the number of accessible children in the object. If all of the children of this object implement Accessible, than this method should return the number of children of this object.

Returns:int

the number of accessible children in the object.

getAccessibleIconDescriptionback to summary
public String getAccessibleIconDescription()

Implements javax.accessibility.AccessibleIcon.getAccessibleIconDescription.

Gets the description of the icon. 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 icon.

Returns:String

the description of the icon

getAccessibleIconHeightback to summary
public int getAccessibleIconHeight()

Implements javax.accessibility.AccessibleIcon.getAccessibleIconHeight.

Gets the height of the icon.

Returns:int

the height of the icon

getAccessibleIconWidthback to summary
public int getAccessibleIconWidth()

Implements javax.accessibility.AccessibleIcon.getAccessibleIconWidth.

Gets the width of the icon.

Returns:int

the width of the icon

getAccessibleIndexInParentback to summary
public int getAccessibleIndexInParent()

Implements abstract javax.accessibility.AccessibleContext.getAccessibleIndexInParent.

Gets the index of this object in its accessible parent.

Returns:int

the index of this object in its parent; -1 if this object does not have an accessible parent.

See Also
getAccessibleParent
getAccessibleParentback to summary
public Accessible getAccessibleParent()

Overrides javax.accessibility.AccessibleContext.getAccessibleParent.

Gets the Accessible parent of this object. If the parent of this object implements Accessible, this method should simply return getParent().

Returns:Accessible

the Accessible parent of this object -- can be null if this object does not have an Accessible parent

getAccessibleRoleback to summary
public AccessibleRole getAccessibleRole()

Implements abstract javax.accessibility.AccessibleContext.getAccessibleRole.

Gets the role of this object.

Returns:AccessibleRole

an instance of AccessibleRole describing the role of the object

See Also
AccessibleRole
getAccessibleStateSetback to summary
public AccessibleStateSet getAccessibleStateSet()

Implements abstract javax.accessibility.AccessibleContext.getAccessibleStateSet.

Gets the state of this object.

Returns:AccessibleStateSet

an instance of AccessibleStateSet containing the current state set of the object

See Also
AccessibleState
getLocaleback to summary
public Locale getLocale() throws IllegalComponentStateException

Implements abstract javax.accessibility.AccessibleContext.getLocale.

Returns the locale of this object.

Returns:Locale

the locale of this object

Exceptions
IllegalComponentStateException:

Doc from javax.accessibility.AccessibleContext.getLocale.

If the component does not have its own locale and has not yet been added to a containment hierarchy such that the locale can be determined from the containing parent

readObjectback to summary
private void readObject(ObjectInputStream s) throws ClassNotFoundException, IOException
Annotations
@Serial
setAccessibleIconDescriptionback to summary
public void setAccessibleIconDescription(String description)

Implements javax.accessibility.AccessibleIcon.setAccessibleIconDescription.

Sets the description of the icon. 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 icon.

Parameters
description:String

the description of the icon

writeObjectback to summary
private void writeObject(ObjectOutputStream s) throws IOException
Annotations
@Serial