Previous | Next | Trail Map | To 1.1 -- And Beyond! | GUI Changes: The AWT Grows Up


How to Use Labels

With the JLabel(in the API reference documentation) class, you can display unselectable text and images. Labels can't get the keyboard focus. [ANYTHING ELSE?]

Here's a picture of an application that displays three labels. The window is divided into three rows of equal height; the label in each row is as wide as possible.


Try this:
  1. Compile and run the application. The source file is LabelDemo.java.
    See Getting Started with Swing if you need help.
  2. Resize the window so you can see how the labels align.
    All the labels have the default vertical alignment -- they're in the vertical center of their layout space. The top level label, which contains both image and text, is specified to have horizontal center alignment. The second label, which contains just text, has the left alignment that is the default for text-only labels. The third label, which contains just an image, has horizontal center alignment, which is the default for image-only labels.

Below is the code from LabelDemo.java that creates the labels in the previous example.

ImageIcon icon = new ImageIcon("middle.gif");
. . .
label1 = new JLabel("Image and Text",
		    icon,
		    JLabel.CENTER);
label1.setVerticalTextPosition(JLabel.BOTTOM);
label1.setHorizontalTextPosition(JLabel.CENTER);

label2 = new JLabel("Text-Only Label");

label3 = new JLabel(icon);

//Add labels to the JPanel. 
add(label1);
add(label2);
add(label3);
The following tables list the commonly used JLabel constructors and methods. Other methods you're likely to call are defined by the Component(in the API reference documentation) class. They include setFont and setForeground. [link to Component discussion.]

The API for using labels falls into two categories:

Setting or Getting the Label's Contents
Method or Constructor Purpose Example
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 defined in the GraphicsUtilsConstants(in the API reference documentation) interface (which JLabel implements): LEFT, CENTER, or RIGHT. LabelDemo.java
void setText(String)
String getText()
Set or get the text displayed by the label. [none yet]
void setIcon(Icon)
Icon getIcon()
Set or get the image displayed by the label. [none yet]
void setRepresentedKeyAccelerator(char)
char getRepresentedKeyAccelerator()
Set or get the letter that should look like a keyboard accelerator. This is handy when a label describes a component (such as a text field) that has a keyboard accelerator but can't display it. [none yet]
void setDisabledIcon(Icon)
Icon getDisabledIcon()
Set or get the image displayed by the label when it's disabled. If you don't specify a disabled image, then the look-and-feel creates one by manipulating the default image. [none yet]

Fine Tuning the Label's Appearance
Method Purpose Example
void setHorizontalAlignment(int)
void setVerticalAlignment(int)
int getHorizontalAlignment()
int getVerticalAlignment()
Set or get where in the label its contents should be placed. The GraphicsUtilsConstants(in the API reference documentation) interface defines three possible values for horizontal alignment: LEFT (the default for text-only labels), CENTER (the default for image-only labels), or RIGHT. For vertical alignment: TOP, CENTER (the default), and BOTTOM. [none yet]
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 GraphicsUtilsConstants(in the API reference documentation) interface defines three possible values for horizontal position: LEFT, CENTER, and RIGHT (the default). For vertical position: TOP, CENTER (the default), and BOTTOM. LabelDemo.java
void setIconTextGap(int)
int getIconTextGap()
Set or get the number of pixels between the label's text and its image. [none yet]


Previous | Next | Trail Map | To 1.1 -- And Beyond! | GUI Changes: The AWT Grows Up