java.awt
Class Label

java.lang.Object
  extended byjava.awt.Component
      extended byjava.awt.Label
All Implemented Interfaces:
javax.accessibility.Accessible, ImageObserver, MenuContainer, Serializable

public class Label
extends Component
implements javax.accessibility.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'

Since:
JDK1.0
Author:
Sami Shaio
See Also:
Serialized Form

Nested Class Summary
protected  class Label.AccessibleAWTLabel
          This class implements accessibility support for the Label class.
 
Nested classes inherited from class java.awt.Component
Component.AccessibleAWTComponent, Component.AWTTreeLock, Component.BltBufferStrategy, Component.FlipBufferStrategy
 
Field Summary
(package private)  int alignment
          The label's alignment.
private static String base
           
static int CENTER
          Indicates that the label should be centered.
static int LEFT
          Indicates that the label should be left justified.
private static int nameCounter
           
static int RIGHT
          Indicates that the label should be right justified.
private static long serialVersionUID
           
(package private)  String text
          The text of this label.
 
Fields inherited from class java.awt.Component
accessibleContext, actionListenerK, adjustmentListenerK, appContext, background, BOTTOM_ALIGNMENT, bufferStrategy, CENTER_ALIGNMENT, componentListener, componentListenerK, componentOrientation, containerListenerK, cursor, dropTarget, enabled, eventMask, focusListener, focusListenerK, focusTraversalKeys, font, foreground, graphicsConfig, height, hierarchyBoundsListener, hierarchyBoundsListenerK, hierarchyListener, hierarchyListenerK, ignoreRepaint, incRate, inputMethodListener, inputMethodListenerK, isInc, isPacked, itemListenerK, keyListener, keyListenerK, LEFT_ALIGNMENT, locale, LOCK, metrics, minSize, mouseListener, mouseListenerK, mouseMotionListener, mouseMotionListenerK, mouseWheelListener, mouseWheelListenerK, newEventsOnly, ownedWindowK, parent, peer, peerFont, popups, prefSize, RIGHT_ALIGNMENT, textListenerK, TOP_ALIGNMENT, valid, visible, width, windowClosingException, windowFocusListenerK, windowListenerK, windowStateListenerK, x, y
 
Fields inherited from interface java.awt.image.ImageObserver
ABORT, ALLBITS, ERROR, FRAMEBITS, HEIGHT, PROPERTIES, SOMEBITS, WIDTH
 
Constructor Summary
Label()
          Constructs an empty label.
Label(String text)
          Constructs a new label with the specified string of text, left justified.
Label(String text, int alignment)
          Constructs a new label that presents the specified string of text with the specified alignment.
 
Method Summary
 void addNotify()
          Creates the peer for this label.
(package private)  String constructComponentName()
          Construct a name for this component.
 javax.accessibility.AccessibleContext getAccessibleContext()
          Gets the AccessibleContext associated with this Label.
 int getAlignment()
          Gets the current alignment of this label.
 String getText()
          Gets the text of this label.
private static void initIDs()
          Initialize JNI field and method IDs
protected  String paramString()
          Returns a string representing the state of this Label.
private  void readObject(ObjectInputStream s)
          Read a label from an object input stream.
 void setAlignment(int alignment)
          Sets the alignment for this label to the specified alignment.
 void setText(String text)
          Sets the text for this label to the specified text.
 
Methods inherited from class java.awt.Component
action, add, addComponentListener, addFocusListener, addHierarchyBoundsListener, addHierarchyListener, addInputMethodListener, addKeyListener, addMouseListener, addMouseMotionListener, addMouseWheelListener, addPropertyChangeListener, addPropertyChangeListener, adjustListeningChildrenOnParent, applyComponentOrientation, areFocusTraversalKeysSet, areInputMethodsEnabled, autoProcessMouseWheel, autoTransferFocus, bounds, checkGD, checkImage, checkImage, checkWindowClosingException, clearCurrentFocusCycleRootOnHide, clearMostRecentFocusOwnerOnHide, coalesceEvents, contains, contains, containsFocus, countHierarchyMembers, createBufferStrategy, createBufferStrategy, createChildHierarchyEvents, createHierarchyEvents, createImage, createImage, createVolatileImage, createVolatileImage, deliverEvent, disable, disableEvents, dispatchEvent, dispatchEventImpl, dispatchMouseWheelToAncestor, doLayout, enable, enable, enableEvents, enableInputMethods, eventEnabled, eventTypeEnabled, firePropertyChange, firePropertyChange, firePropertyChange, getAccessibleIndexInParent, getAccessibleStateSet, getAlignmentX, getAlignmentY, getBackBuffer, getBackground, getBounds, getBounds, getBufferStrategy, getColorModel, getComponentAt, getComponentAt, getComponentListeners, getComponentOrientation, getCursor, getDropTarget, getFocusCycleRootAncestor, getFocusListeners, getFocusTraversalKeys_NoIDCheck, getFocusTraversalKeys, getFocusTraversalKeysEnabled, getFont_NoClientCode, getFont, getFontMetrics, getForeground, getGraphics, getGraphicsConfiguration, getHeight, getHierarchyBoundsListeners, getHierarchyListeners, getIgnoreRepaint, getInputContext, getInputMethodListeners, getInputMethodRequests, getKeyListeners, getListeners, getLocale, getLocation, getLocation, getLocationOnScreen_NoTreeLock, getLocationOnScreen, getMaximumSize, getMinimumSize, getMouseListeners, getMouseMotionListeners, getMouseWheelListeners, getName, getNativeContainer, getParent_NoClientCode, getParent, getPeer, getPreferredSize, getPropertyChangeListeners, getPropertyChangeListeners, getSize, getSize, getToolkit, getToolkitImpl, getTreeLock, getWidth, getX, getY, gotFocus, handleEvent, hasFocus, hide, imageUpdate, initializeFocusTraversalKeys, inside, invalidate, isBackgroundSet, isCursorSet, isDisplayable, isDoubleBuffered, isEnabled, isEnabledImpl, isFocusable, isFocusCycleRoot, isFocusOwner, isFocusTraversable, isFocusTraversableOverridden, isFontSet, isForegroundSet, isLightweight, isOpaque, isRecursivelyVisible, isShowing, isValid, isVisible, keyDown, keyUp, layout, lightweightPaint, lightweightPrint, list, list, list, list, list, locate, location, lostFocus, minimumSize, mouseDown, mouseDrag, mouseEnter, mouseExit, mouseMove, mouseUp, move, nextFocus, nextFocusHelper, numListening, paint, paintAll, paintHeavyweightComponents, postEvent, postsOldMouseEvents, preferredSize, prepareImage, prepareImage, print, printAll, printHeavyweightComponents, processComponentEvent, processEvent, processFocusEvent, processHierarchyBoundsEvent, processHierarchyEvent, processInputMethodEvent, processKeyEvent, processMouseEvent, processMouseMotionEvent, processMouseWheelEvent, remove, removeComponentListener, removeFocusListener, removeHierarchyBoundsListener, removeHierarchyListener, removeInputMethodListener, removeKeyListener, removeMouseListener, removeMouseMotionListener, removeMouseWheelListener, removeNotify, removePropertyChangeListener, removePropertyChangeListener, repaint, repaint, repaint, repaint, requestFocus, requestFocus, requestFocusHelper, requestFocusInWindow, requestFocusInWindow, resetGC, reshape, resize, resize, setBackground, setBounds, setBounds, setComponentOrientation, setCursor, setDropTarget, setEnabled, setFocusable, setFocusTraversalKeys_NoIDCheck, setFocusTraversalKeys, setFocusTraversalKeysEnabled, setFont, setForeground, setGCFromPeer, setIgnoreRepaint, setLocale, setLocation, setLocation, setName, setSize, setSize, setVisible, show, show, size, toString, transferFocus, transferFocusBackward, transferFocusUpCycle, update, updateCursorImmediately, validate
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

LEFT

public static final int LEFT
Indicates that the label should be left justified.

See Also:
Constant Field Values

CENTER

public static final int CENTER
Indicates that the label should be centered.

See Also:
Constant Field Values

RIGHT

public static final int RIGHT
Indicates that the label should be right justified.

Since:
JDK1.0t.
See Also:
Constant Field Values

text

String text
The text of this label. This text can be modified by the program but never by the user.

See Also:
getText(), setText(String)

alignment

int alignment
The label's alignment. The default alignment is set to be left justified.

See Also:
getAlignment(), setAlignment(int)

base

private static final String base
See Also:
Constant Field Values

nameCounter

private static int nameCounter

serialVersionUID

private static final long serialVersionUID
See Also:
Constant Field Values
Constructor Detail

Label

public Label()
      throws HeadlessException
Constructs an empty label. The text of the label is the empty string "".

Throws:
HeadlessException - if GraphicsEnvironment.isHeadless() returns true.
See Also:
GraphicsEnvironment.isHeadless()

Label

public Label(String text)
      throws HeadlessException
Constructs a new label with the specified string of text, left justified.

Parameters:
text - the string that the label presents. A null value will be accepted without causing a NullPointerException to be thrown.
Throws:
HeadlessException - if GraphicsEnvironment.isHeadless() returns true.
See Also:
GraphicsEnvironment.isHeadless()

Label

public Label(String text,
             int alignment)
      throws HeadlessException
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.

Parameters:
text - the string that the label presents. A null value will be accepted without causing a NullPointerException to be thrown.
alignment - the alignment value.
Throws:
HeadlessException - if GraphicsEnvironment.isHeadless() returns true.
See Also:
GraphicsEnvironment.isHeadless()
Method Detail

readObject

private void readObject(ObjectInputStream s)
                 throws ClassNotFoundException,
                        IOException,
                        HeadlessException
Read a label from an object input stream.

Throws:
HeadlessException - if GraphicsEnvironment.isHeadless() returns true
ClassNotFoundException
IOException
Since:
1.4
See Also:
GraphicsEnvironment.isHeadless()

constructComponentName

String constructComponentName()
Construct a name for this component. Called by getName() when the name is null.

Overrides:
constructComponentName in class Component

addNotify

public void addNotify()
Creates the peer for this label. The peer allows us to modify the appearance of the label without changing its functionality.

Overrides:
addNotify in class Component
See Also:
Component.isDisplayable(), Component.removeNotify()

getAlignment

public int getAlignment()
Gets the current alignment of this label. Possible values are Label.LEFT, Label.RIGHT, and Label.CENTER.

See Also:
setAlignment(int)

setAlignment

public void setAlignment(int alignment)
Sets the alignment for this label to the specified alignment. Possible values are Label.LEFT, Label.RIGHT, and Label.CENTER.

Parameters:
alignment - the alignment to be set.
Throws:
IllegalArgumentException - if an improper value for alignment is given.
See Also:
getAlignment()

getText

public String getText()
Gets the text of this label.

Returns:
the text of this label, or null if the text has been set to null.
See Also:
setText(java.lang.String)

setText

public void setText(String text)
Sets the text for this label to the specified text.

Parameters:
text - the text that this label displays. If text is null, it is treated for display purposes like an empty string "".
See Also:
getText()

paramString

protected String paramString()
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.

Overrides:
paramString in class Component
Returns:
the parameter string of this label

initIDs

private static void initIDs()
Initialize JNI field and method IDs


getAccessibleContext

public javax.accessibility.AccessibleContext getAccessibleContext()
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.

Specified by:
getAccessibleContext in interface javax.accessibility.Accessible
Overrides:
getAccessibleContext in class Component
Returns:
an AccessibleAWTLabel that serves as the AccessibleContext of this Label