Working with Images

JLabels make it very simple to add graphics to your user interface. Images used in JLabels (and also in other Swing components, such as buttons) are of type javax.swing.Icon, an interface described in detail in the next section.
These two lines of code show how simple it is to create a label containing an image:

ImageIcon icon = new ImageIcon("smile.gif");
JLabel label = new JLabel(icon);

For labels that contain both graphics and text, Swing provides considerable flexibility with respect to the relative location of the text and image. The text for the label may be displayed at any one of nine locations relative to the image. These locations are specified via the setVerticalTextPosition( ) and setHorizontalTextPosition( ) methods, which take values from the SwingConstants class discussed earlier. Note the distinction between the label’s text position and its alignment; text position reflects the position of the text relative to the image while alignment specifies the location of the label’s contents (image and text) relative to the borders of the label.
Another useful feature of the JLabel class is the ability to enable and disable the label by “graying out” the label and text. By default, a call to JLabel.setEnabled(false) switches the image to an automatically generated grayscale version of the original image and alters the text rendering in some (L&F-specific) way. However, the grayscale image is used only if no disabled icon has been set. The setDisabledIcon( ) method can be used to set an alternate image for the disabled label.
Additionally, the spacing between the image and the text can be specified by a call to setIconTextGap( ), which takes a single parameter specifying the number of pixels between the image and the icon. This setting has no effect if both the horizontal and vertical text positions are set to SwingConstants.CENTER since, in this case, the text is placed directly over the image.

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

public class ImageLabelExample {

	private static Icon icon = new ImageIcon("smile.gif");

	public static void main(String[] args) {
		JLabel[] labels = new JLabel[9];

		labels[0] = makeLabel(JLabel.TOP, JLabel.LEFT);
		labels[1] = makeLabel(JLabel.TOP, JLabel.CENTER);
		labels[2] = makeLabel(JLabel.TOP, JLabel.RIGHT);
		labels[3] = makeLabel(JLabel.CENTER, JLabel.LEFT);
		labels[4] = makeLabel(JLabel.CENTER, JLabel.CENTER);
		labels[5] = makeLabel(JLabel.CENTER, JLabel.RIGHT);
		labels[6] = makeLabel(JLabel.BOTTOM, JLabel.LEFT);
		labels[7] = makeLabel(JLabel.BOTTOM, JLabel.CENTER);
		labels[8] = makeLabel(JLabel.BOTTOM, JLabel.RIGHT);

		// Disable label 0.

		// Disable label 1 with a disabled icon.
		labels[1].setDisabledIcon(new ImageIcon("images/no.gif"));

		// Change text gap on labels 2 and 3.

		// Add the labels to a frame and display it.
		JFrame frame = new JFrame();
		Container c = frame.getContentPane();
		c.setLayout(new FlowLayout(FlowLayout.CENTER, 3, 3));
		for (int i = 0; i < 9; i++)
		frame.setSize(350, 150);

	protected static JLabel makeLabel(int vert, int horiz) {
		JLabel l = new JLabel("Smile", icon, SwingConstants.CENTER);
		return l;

You may also like...