FileDocCategorySizeDatePackage
Label.javaAPI DocJava SE 6 API10050Tue Jun 10 00:25:16 BST 2008java.awt

Label

public class Label extends Component implements Accessible
A Label object is a component for placing text in a container. A label displays a single line of read-only text. The text can be changed by the application, but a user cannot edit it directly.

For example, the code . . .


setLayout(new FlowLayout(FlowLayout.CENTER, 10, 10));
add(new Label("Hi There!"));
add(new Label("Another Label"));

produces the following labels:

Two labels: 'Hi There!' and 'Another label'

version
1.59, 04/07/06
author
Sami Shaio
since
JDK1.0

Fields Summary
public static final int
LEFT
Indicates that the label should be left justified.
public static final int
CENTER
Indicates that the label should be centered.
public static final int
RIGHT
Indicates that the label should be right justified.
String
text
The text of this label. This text can be modified by the program but never by the user.
int
alignment
The label's alignment. The default alignment is set to be left justified.
private static final String
base
private static int
nameCounter
private static final long
serialVersionUID
Constructors Summary
public Label()
Constructs an empty label. The text of the label is the empty string "".

exception
HeadlessException if GraphicsEnvironment.isHeadless() returns true.
see
java.awt.GraphicsEnvironment#isHeadless


                               
        
	this("", LEFT);
    
public Label(String text)
Constructs a new label with the specified string of text, left justified.

param
text the string that the label presents. A null value will be accepted without causing a NullPointerException to be thrown.
exception
HeadlessException if GraphicsEnvironment.isHeadless() returns true.
see
java.awt.GraphicsEnvironment#isHeadless

        this(text, LEFT);
    
public Label(String text, int alignment)
Constructs a new label that presents the specified string of text with the specified alignment. Possible values for alignment are Label.LEFT, Label.RIGHT, and Label.CENTER.

param
text the string that the label presents. A null value will be accepted without causing a NullPointerException to be thrown.
param
alignment the alignment value.
exception
HeadlessException if GraphicsEnvironment.isHeadless() returns true.
see
java.awt.GraphicsEnvironment#isHeadless

        GraphicsEnvironment.checkHeadless();
	this.text = text;
	setAlignment(alignment);
    
Methods Summary
public voidaddNotify()
Creates the peer for this label. The peer allows us to modify the appearance of the label without changing its functionality.

        synchronized (getTreeLock()) {
	    if (peer == null)
	        peer = getToolkit().createLabel(this);
	    super.addNotify();
	}
    
java.lang.StringconstructComponentName()
Construct a name for this component. Called by getName() when the name is null.

        synchronized (getClass()) {
	    return base + nameCounter++;
	}
    
public javax.accessibility.AccessibleContextgetAccessibleContext()
Gets the AccessibleContext associated with this Label. For labels, the AccessibleContext takes the form of an AccessibleAWTLabel. A new AccessibleAWTLabel instance is created if necessary.

return
an AccessibleAWTLabel that serves as the AccessibleContext of this Label
since
1.3

        if (accessibleContext == null) {
            accessibleContext = new AccessibleAWTLabel();
        }
        return accessibleContext;
    
public intgetAlignment()
Gets the current alignment of this label. Possible values are Label.LEFT, Label.RIGHT, and Label.CENTER.

see
java.awt.Label#setAlignment

	return alignment;
    
public java.lang.StringgetText()
Gets the text of this label.

return
the text of this label, or null if the text has been set to null.
see
java.awt.Label#setText

	return text;
    
private static native voidinitIDs()
Initialize JNI field and method IDs

protected java.lang.StringparamString()
Returns a string representing the state of this Label. This method is intended to be used only for debugging purposes, and the content and format of the returned string may vary between implementations. The returned string may be empty but may not be null.

return
the parameter string of this label

	String str = ",align=";
	switch (alignment) {
	  case LEFT:   str += "left"; break;
	  case CENTER: str += "center"; break;
	  case RIGHT:  str += "right"; break;
	}
	return super.paramString() + str + ",text=" + text;
    
private voidreadObject(java.io.ObjectInputStream s)
Read a label from an object input stream.

exception
HeadlessException if GraphicsEnvironment.isHeadless() returns true
serial
since
1.4
see
java.awt.GraphicsEnvironment#isHeadless

        GraphicsEnvironment.checkHeadless();
        s.defaultReadObject();
    
public synchronized voidsetAlignment(int alignment)
Sets the alignment for this label to the specified alignment. Possible values are Label.LEFT, Label.RIGHT, and Label.CENTER.

param
alignment the alignment to be set.
exception
IllegalArgumentException if an improper value for alignment is given.
see
java.awt.Label#getAlignment

	switch (alignment) {
	  case LEFT:
	  case CENTER:
	  case RIGHT:
	    this.alignment = alignment;
    	    LabelPeer peer = (LabelPeer)this.peer;
	    if (peer != null) {
		peer.setAlignment(alignment);
	    }
	    return;
	}
	throw new IllegalArgumentException("improper alignment: " + alignment);
    
public voidsetText(java.lang.String text)
Sets the text for this label to the specified text.

param
text the text that this label displays. If text is null, it is treated for display purposes like an empty string "".
see
java.awt.Label#getText

        boolean testvalid = false;
	synchronized (this) {
	    if (text != this.text && (this.text == null ||
				      !this.text.equals(text))) {
	        this.text = text;
		LabelPeer peer = (LabelPeer)this.peer;
		if (peer != null) {
		    peer.setText(text);
		}
		testvalid = true;
	    }
	}

	// This could change the preferred size of the Component.
	if (testvalid && valid) {
	    invalidate();
	}