Java Reference
In-Depth Information
unlike
Image
objects,
ImageIcon
objects are serializable so that they can be easily used by
JavaBean components.
Creating an ImageIcon
There are nine constructors for an
ImageIcon
:
public ImageIcon()
Icon icon = new ImageIcon();
icon.setImage(anImage);
public ImageIcon(Image image)
Icon icon = new ImageIcon(anImage);
public ImageIcon(String filename)
Icon icon = new ImageIcon(filename);
public ImageIcon(URL location)
Icon icon = new ImageIcon(url);
public ImageIcon(byte imageData[])
Icon icon = new ImageIcon(aByteArray);
public ImageIcon(Image image, String description)
Icon icon = new ImageIcon(anImage, "Duke");
public ImageIcon(String filename, String description)
Icon icon = new ImageIcon(filename, filename);
public ImageIcon(URL location, String description)
Icon icon = new ImageIcon(url, location.getFile());
public ImageIcon(byte imageData[], String description)
Icon icon = new ImageIcon(aByteArray, "Duke");
The no-argument version creates an uninitialized version (empty). The remaining eight
offer the ability to create an
ImageIcon
from an
Image
,
byte
array, file name
String
, or
URL
, with
or without a description.
Using an ImageIcon
Using an
ImageIcon
is as simple as using an
Icon
: just create the
ImageIcon
and associate it with
a component.
Icon icon = new ImageIcon("Warn.gif");
JLabel label3 = new JLabel("Warning", icon, JLabel.CENTER)