Label

In this notes we will discribe how to use JLabel in more detail.
You must read How to Use Labels.

How to use JLabel

HelloKittyFrame.java

In this program, we are going to learn how to add images to JLabel You can get the program here.
You have to download the picture. hellokitty.gif
The output of the program is
hellokittyframe.gif
   1:import javax.swing.*;
   2:import java.awt.*;
   3:
   4:public class HelloKittyFrame extends JFrame {
   5:    // 2-dimensional array
   6:    private JLabel HelloKittyLabels[][] = new JLabel[3][3];
   7:    // file name of the image
   8:    private String imageFileName = "hellokitty.gif";
   9:    // text to be displayed on labels
  10:    private String text = "Hello Kitty";
  11:    // icon 
  12:    private ImageIcon icon = new ImageIcon(imageFileName);
  13:    private Container contentPane = getContentPane();
  14:    public HelloKittyFrame() {
  15:        super("Hello Kitty");
  16:        int row = 3, col = 3;
  17:        contentPane.setLayout(new GridLayout(row, col));
  18:        // initiate the labels and add to the frame
  19:        for(int i = 0; i < 3; i++) {
  20:            for(int j = 0; j < 3; j++) {
  21:                HelloKittyLabels[i][j] = new JLabel(text, icon, JLabel.CENTER);
  22:                contentPane.add(HelloKittyLabels[i][j]);
  23:            }
  24:        }
  25:        // set horizontal text position and vertical text position
  26:        HelloKittyLabels[0][0].setHorizontalTextPosition(JLabel.LEFT);
  27:        HelloKittyLabels[0][0].setVerticalTextPosition(JLabel.TOP);
  28:        HelloKittyLabels[0][1].setHorizontalTextPosition(JLabel.CENTER);
  29:        HelloKittyLabels[0][1].setVerticalTextPosition(JLabel.TOP);
  30:        HelloKittyLabels[0][2].setHorizontalTextPosition(JLabel.RIGHT);
  31:        HelloKittyLabels[0][2].setVerticalTextPosition(JLabel.TOP);
  32:        HelloKittyLabels[1][0].setHorizontalTextPosition(JLabel.LEFT);
  33:        HelloKittyLabels[1][0].setVerticalTextPosition(JLabel.CENTER);
  34:        HelloKittyLabels[1][1].setHorizontalTextPosition(JLabel.CENTER);
  35:        HelloKittyLabels[1][1].setVerticalTextPosition(JLabel.CENTER);
  36:        HelloKittyLabels[1][2].setHorizontalTextPosition(JLabel.RIGHT);
  37:        HelloKittyLabels[1][2].setVerticalTextPosition(JLabel.CENTER);
  38:        HelloKittyLabels[2][0].setHorizontalTextPosition(JLabel.LEFT);
  39:        HelloKittyLabels[2][0].setVerticalTextPosition(JLabel.BOTTOM);
  40:        HelloKittyLabels[2][1].setHorizontalTextPosition(JLabel.CENTER);
  41:        HelloKittyLabels[2][1].setVerticalTextPosition(JLabel.BOTTOM);
  42:        HelloKittyLabels[2][2].setHorizontalTextPosition(JLabel.RIGHT);
  43:        HelloKittyLabels[2][2].setVerticalTextPosition(JLabel.BOTTOM);
  44:    }
  45:    
  46:    public static void main(String[] args) {
  47:        HelloKittyFrame f = new HelloKittyFrame();
  48:        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  49:        f.setSize(600,500);
  50:        f.setVisible(true);
  51:    }
  52:}

ImageIcon

ImageIcon is a class in package javax.swing. It represents an icon. It make an icon from an image file such as x.gif or y.jpg . One constructor is
ImageIcon(String filename)
Here the filename is the file name of your image.
In the program line 12, we created an image icon.

Constructors

JLabel()
JLabel(Icon icon)
JLabel(Icon icon, int horizontalAlignment)
JLabel(String text)
JLabel(String text, Icon icon, int horizontalAlignment)
JLabel(String text, int horizontalAlignment)
The meaning of the constructors should be clear. text is the text displayed on the label. icon is the image. horizontalAlignment is the alignment of the content.

setVerticalTextPosition

We use setVerticalTextPosition(int) to set the position of the text relative to the image. The value can be JLabel.TOP, JLabel.CENTER, JLabel.BOTTOM.
Similarly we use setHorizontalTextPosition(int) to set the position fo the text relative to the image. The value can be JLabel.LEFT, JLabel.CENTER, JLabel.RIGHT. The following table summarize the useful methods of JLabel:
Setting or Getting the Label's Contents
Method or Constructor Purpose
JLabel(Icon)
JLabel(Icon, int)
JLabel(String)
JLabel(String, Icon, int)
JLabel(String, int)
JLabel()
Create a JLabel instance, initializing it to have the specified text/image/alignment. The int argument specifies the horizontal alignment of the label's contents within its drawing area. The horizontal alignment must be one of the following constants LEFT, CENTER, RIGHT, LEADING, or TRAILING.
void setText(String)
String getText()
Set or get the text displayed by the label.
void setIcon(Icon)
Icon getIcon()
Set or get the image displayed by the label.

Fine Tuning the Label's Appearance
Method Purpose
void setHorizontalAlignment(int)
void setVerticalAlignment(int)
int getHorizontalAlignment()
int getVerticalAlignment()
Set or get where in the label its contents should be placed. The possible values can be LEFT (the default for image-only labels), CENTER (the default for text-only labels), RIGHT, LEADING, and TRAILING. For vertical alignment: TOP, CENTER (the default), and BOTTOM.
void setHorizontalTextPosition(int)
void setVerticalTextPosition(int)
int getHorizontalTextPosition()
int getVerticalTextPosition()
Set or get where the button's text should be placed, relative to the button's image. The possible values for horizontal position: LEFT, CENTER, and RIGHT (the default). For vertical position: TOP, CENTER (the default), and BOTTOM.
void setIconTextGap(int)
int getIconTextGap()
Set or get the number of pixels between the label's text and its image.

Question :

homer
Homer
marge
Marge
bart
Bart
lisa
Lisa
maggie
Maggie
Write a class SimpsonLabel. It extends JLabel. It has a constructor SimpsonLabel(String name) which creates a label with the corresponding image at the center.

Answer : SimpsonLabel.java

You can get the program here.
Remember to download the above Simpson pictures.
   1:import java.awt.*;
   2:import javax.swing.*;
   3:
   4:public class SimpsonLabel extends JLabel {
   5:    private String name; // Homer,  Marge, Bart, Lisa, Maggie, 
   6:    private String[] nameList = {"Homer", "Marge", "Bart", "Lisa", "Maggie"};
   7:    // the file names of the images
   8:    private String[] filenames = {"homer.gif", "marge.gif", "bart.gif", 
   9:                                  "lisa.gif", "maggie.gif"};
  10:    private Icon image;
  11:    public SimpsonLabel(String name) {
  12:        this.name = name;
  13:        setUpImage();
  14:    }
  15:    
  16:    // set up the image 
  17:    private void setUpImage() {
  18:        for(int i = 0; i < nameList.length; i++) {
  19:            if(name.equals(nameList[i])) { // if name is found
  20:                image = new ImageIcon(filenames[i]); // set the image
  21:                this.setIcon(image); // set the icon of the Label
  22:                this.setHorizontalAlignment(JLabel.CENTER);
  23:                return; // stop the method 
  24:            }
  25:        }
  26:        // no match is found
  27:        this.setText("Unknown");
  28:    }
  29:}

Highlight

SimpsonTest.java

You can get the program here.
simpsontest.gif.
   1:import java.awt.*;
   2:import javax.swing.*;
   3:
   4:public class SimpsonTest  {
   5:    public static void main(String[] args) {
   6:        JFrame f = new JFrame("Simpson");
   7:        f.getContentPane().setLayout(new GridLayout(1,0));
   8:        String[] name = {"Homer", "Lisa", "Maggie", "Bart", "Marge"};
   9:        for(int i = 0; i < name.length; i++) {
  10:            f.getContentPane().add(new SimpsonLabel(name[i]));
  11:        }
  12:        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  13:        f.setSize(250,70);
  14:        f.setVisible(true);
  15:    }
  16:}