APIdia Launch Partner
Top Description Inners Fields Constructors Methods
java.awt

public Class Label

extends Component
implements Accessible
Class Inheritance
All Implemented Interfaces
javax.accessibility.Accessible
Imports
java.awt.peer.LabelPeer, java.io.IOException, .ObjectInputStream, .Serial, javax.accessibility.Accessible, .AccessibleContext, .AccessibleRole

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'

Author
Sami Shaio
Since
1.0

Nested and Inner Type Summary

Modifier and TypeClass and Description
protected class
Label.AccessibleAWTLabel

This class implements accessibility support for the Label class.

Field Summary

Modifier and TypeField and Description
pack-priv int
alignment

The label's alignment.

private static final String
public static final int
CENTER

Indicates that the label should be centered.

public static final int
LEFT

Indicates that the label should be left justified.

private static int
public static final int
RIGHT

Indicates that the label should be right justified.

private static final long
serialVersionUID

Hides java.awt.Component.serialVersionUID.

Use serialVersionUID from JDK 1.1 for interoperability.
pack-priv String
text

The text of this label.

Inherited from java.awt.Component:
accessibleContextactionListenerKadjustmentListenerKbackgroundbackgroundEraseDisabledBOTTOM_ALIGNMENTCENTER_ALIGNMENTcomponentListenercomponentListenerKcomponentOrientationcontainerListenerKcursordropTargetenabledeventCacheeventMaskfocusListenerfocusListenerKfocusTraversalKeysfontforegroundheighthierarchyBoundsListenerhierarchyBoundsListenerKhierarchyListenerhierarchyListenerKignoreRepaintincRateinputMethodListenerinputMethodListenerKisIncisPackeditemListenerKkeyListenerkeyListenerKLEFT_ALIGNMENTlocaleLOCKmaxSizemaxSizeSetminSizeminSizeSetmouseListenermouseListenerKmouseMotionListenermouseMotionListenerKmouseWheelListenermouseWheelListenerKnewEventsOnlyownedWindowKparentpeerpeerFontpopupsprefSizeprefSizeSetRIGHT_ALIGNMENTtextListenerKTOP_ALIGNMENTvisiblewidthwindowFocusListenerKwindowListenerKwindowStateListenerKxy

Constructor Summary

AccessConstructor and Description
public
Label()

Constructs an empty label.

public
Label(String
the string that the label presents. A null value will be accepted without causing a NullPointerException to be thrown.
text
)

Constructs a new label with the specified string of text, left justified.

public
Label(String
the string that the label presents. A null value will be accepted without causing a NullPointerException to be thrown.
text
,
int
the alignment value.
alignment
)

Constructs a new label that presents the specified string of text with the specified alignment.

Method Summary

Modifier and TypeMethod and Description
public void
addNotify()

Overrides java.awt.Component.addNotify.

Creates the peer for this label.

pack-priv String
constructComponentName()

Overrides java.awt.Component.constructComponentName.

Construct a name for this component.

public AccessibleContext

Returns:

an AccessibleAWTLabel that serves as the AccessibleContext of this Label
getAccessibleContext
()

Overrides java.awt.Component.getAccessibleContext.

Implements javax.accessibility.Accessible.getAccessibleContext.

Gets the AccessibleContext associated with this Label.

public int

Returns:

the alignment of this label
getAlignment
()

Gets the current alignment of this label.

public String

Returns:

the text of this label, or null if the text has been set to null.
getText
()

Gets the text of this label.

private static native void
initIDs()

Hides java.awt.Component.initIDs.

Initialize JNI field and method IDs

protected String

Returns:

the parameter string of this label
paramString
()

Overrides java.awt.Component.paramString.

Returns a string representing the state of this Label.

private void
readObject(ObjectInputStream
the ObjectInputStream to read
s
)

Hides java.awt.Component.readObject.

Read a label from an object input stream.

public synchronized void
setAlignment(int
the alignment to be set.
alignment
)

Sets the alignment for this label to the specified alignment.

public void
setText(String
the text that this label displays. If text is null, it is treated for display purposes like an empty string "".
text
)

Sets the text for this label to the specified text.

Inherited from java.awt.Component:
actionaddaddComponentListeneraddFocusListeneraddHierarchyBoundsListeneraddHierarchyListeneraddInputMethodListeneraddKeyListeneraddMouseListeneraddMouseMotionListeneraddMouseWheelListeneraddPropertyChangeListeneraddPropertyChangeListeneradjustListeningChildrenOnParentapplyComponentOrientationapplyCompoundShapeapplyCurrentShapeareBoundsValidareFocusTraversalKeysSetareInputMethodsEnabledautoProcessMouseWheelboundscanBeFocusOwnercanBeFocusOwnerRecursivelycheckGDcheckImagecheckImagecheckTreeLockclearCurrentFocusCycleRootOnHideclearMostRecentFocusOwnerOnHidecoalesceEventscontainscontainscontainsFocuscountHierarchyMemberscreateBufferStrategycreateBufferStrategycreateHierarchyEventscreateImagecreateImagecreateVolatileImagecreateVolatileImagedeliverEventdisabledisableEventsdispatchEventdispatchEventImpldispatchMouseWheelToAncestordoLayoutenableenableenableEventsenableInputMethodseventEnabledeventTypeEnabledfindUnderMouseInWindowfirePropertyChangefirePropertyChangefirePropertyChangefirePropertyChangefirePropertyChangefirePropertyChangefirePropertyChangefirePropertyChangefirePropertyChangegetAccessibleIndexInParentgetAccessibleStateSetgetAlignmentXgetAlignmentYgetBackBuffergetBackgroundgetBaselinegetBaselineResizeBehaviorgetBoundsgetBoundsgetBoundsOpgetBufferStrategygetColorModelgetComponentAtgetComponentAtgetComponentFactorygetComponentListenersgetComponentOrientationgetContainergetContainingWindowgetCursorgetCursor_NoClientCodegetDropTargetgetFocusCycleRootAncestorgetFocusListenersgetFocusTraversalKeysgetFocusTraversalKeys_NoIDCheckgetFocusTraversalKeysEnabledgetFontgetFont_NoClientCodegetFontMetricsgetForegroundgetGraphicsgetGraphics_NoClientCodegetGraphicsConfigurationgetGraphicsConfiguration_NoClientCodegetHeightgetHierarchyBoundsListenersgetHierarchyListenersgetHWPeerAboveMegetIgnoreRepaintgetInputContextgetInputMethodListenersgetInputMethodRequestsgetKeyListenersgetListenersgetLocalegetLocationgetLocationgetLocationOnScreengetLocationOnScreen_NoTreeLockgetLocationOnWindowgetMaximumSizegetMinimumSizegetMouseListenersgetMouseMotionListenersgetMousePositiongetMouseWheelListenersgetNamegetNativeContainergetNextFocusCandidategetNormalShapegetObjectLockgetOpaqueShapegetParentgetParent_NoClientCodegetPreferredSizegetPropertyChangeListenersgetPropertyChangeListenersgetSiblingIndexAbovegetSiblingIndexBelowgetSizegetSizegetToolkitgetToolkitImplgetTraversalRootgetTreeLockgetWidthgetXgetYgotFocushandleEventhasFocushideimageUpdateinitializeFocusTraversalKeysinsideinvalidateinvalidateIfValidinvalidateParentisAutoFocusTransferOnDisposalisBackgroundSetisCoalescingEnabledisCursorSetisDisplayableisDoubleBufferedisEnabledisEnabledImplisFocusableisFocusCycleRootisFocusOwnerisFocusTraversableisFocusTraversableOverriddenisFontSetisForegroundSetisInstanceOfisLightweightisMaximumSizeSetisMinimumSizeSetisMixingNeededisNonOpaqueForMixingisOpaqueisPreferredSizeSetisRecursivelyVisibleisSameOrAncestorOfisShowingisValidisVisibleisVisible_NoClientCodekeyDownkeyUplayoutlightweightPaintlightweightPrintlistlistlistlistlistlocatelocationlostFocusminimumSizemixOnHidingmixOnReshapingmixOnShowingmixOnValidatingmixOnZOrderChangingmouseDownmouseDragmouseEntermouseExitmouseMovemouseUpmovenextFocusnumListeningpaintpaintAllpaintHeavyweightComponentspointRelativeToComponentpostEventpostsOldMouseEventspreferredSizeprepareImageprepareImageprintprintAllprintHeavyweightComponentsprocessComponentEventprocessEventprocessFocusEventprocessHierarchyBoundsEventprocessHierarchyEventprocessInputMethodEventprocessKeyEventprocessMouseEventprocessMouseMotionEventprocessMouseWheelEventrelocateComponentremoveremoveComponentListenerremoveFocusListenerremoveHierarchyBoundsListenerremoveHierarchyListenerremoveInputMethodListenerremoveKeyListenerremoveMouseListenerremoveMouseMotionListenerremoveMouseWheelListenerremoveNotifyremovePropertyChangeListenerremovePropertyChangeListenerrepaintrepaintrepaintrepaintrequestFocusrequestFocusrequestFocusrequestFocusrequestFocusHelperrequestFocusHelperrequestFocusInWindowrequestFocusInWindowrequestFocusInWindowrequestFocusInWindowreshaperesizeresizerevalidaterevalidateSynchronouslysetAutoFocusTransferOnDisposalsetBackgroundsetBoundssetBoundssetBoundsOpsetComponentOrientationsetCursorsetDropTargetsetEnabledsetFocusablesetFocusTraversalKeyssetFocusTraversalKeys_NoIDChecksetFocusTraversalKeysEnabledsetFontsetForegroundsetGraphicsConfigurationsetIgnoreRepaintsetLocalesetLocationsetLocationsetMaximumSizesetMinimumSizesetMixingCutoutShapesetNamesetPreferredSizesetRequestFocusControllersetSizesetSizesetVisibleshowshowsizesubtractAndApplyShapesubtractAndApplyShapeBelowMetoStringtransferFocustransferFocustransferFocusBackwardtransferFocusBackwardtransferFocusUpCycleupdateupdateChildGraphicsDataupdateCursorImmediatelyupdateGraphicsDataupdateZOrdervalidate

Field Detail

alignmentback to summary
pack-priv int alignment

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

See Also
getAlignment(), setAlignment(int)
baseback to summary
private static final String base
CENTERback to summary
public static final int CENTER

Indicates that the label should be centered.

LEFTback to summary
public static final int LEFT

Indicates that the label should be left justified.

nameCounterback to summary
private static int nameCounter
serialVersionUIDback to summary
private static final long serialVersionUID

Hides java.awt.Component.serialVersionUID.

Use serialVersionUID from JDK 1.1 for interoperability.

Annotations
@Serial
textback to summary
pack-priv 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)

Constructor Detail

Labelback to summary
public Label() throws HeadlessException

Constructs an empty label. The text of the label is the empty string "".

Exceptions
HeadlessException:
if GraphicsEnvironment.isHeadless() returns true.
See Also
java.awt.GraphicsEnvironment#isHeadless
Labelback to summary
public Label(String text) throws HeadlessException

Constructs a new label with the specified string of text, left justified.

Parameters
text:String

the string that the label presents. A null value will be accepted without causing a NullPointerException to be thrown.

Exceptions
HeadlessException:
if GraphicsEnvironment.isHeadless() returns true.
See Also
java.awt.GraphicsEnvironment#isHeadless
Labelback to summary
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:String

the string that the label presents. A null value will be accepted without causing a NullPointerException to be thrown.

alignment:int

the alignment value.

Exceptions
HeadlessException:
if GraphicsEnvironment.isHeadless() returns true.
See Also
java.awt.GraphicsEnvironment#isHeadless

Method Detail

addNotifyback to summary
public void addNotify()

Overrides java.awt.Component.addNotify.

Creates the peer for this label. The peer allows us to modify the appearance of the label without changing its functionality.

constructComponentNameback to summary
pack-priv String constructComponentName()

Overrides java.awt.Component.constructComponentName.

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

getAccessibleContextback to summary
public AccessibleContext getAccessibleContext()

Overrides java.awt.Component.getAccessibleContext.

Implements javax.accessibility.Accessible.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.

Returns:AccessibleContext

an AccessibleAWTLabel that serves as the AccessibleContext of this Label

Since
1.3
getAlignmentback to summary
public int getAlignment()

Gets the current alignment of this label. Possible values are Label.LEFT, Label.RIGHT, and Label.CENTER.

Returns:int

the alignment of this label

See Also
java.awt.Label#setAlignment
getTextback to summary
public String getText()

Gets the text of this label.

Returns:String

the text of this label, or null if the text has been set to null.

See Also
java.awt.Label#setText
initIDsback to summary
private static native void initIDs()

Hides java.awt.Component.initIDs.

Initialize JNI field and method IDs

paramStringback to summary
protected String paramString()

Overrides java.awt.Component.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.

Returns:String

the parameter string of this label

readObjectback to summary
private void readObject(ObjectInputStream s) throws ClassNotFoundException, IOException, HeadlessException

Hides java.awt.Component.readObject.

Read a label from an object input stream.

Parameters
s:ObjectInputStream

the ObjectInputStream to read

Annotations
@Serial
Exceptions
ClassNotFoundException:
if the class of a serialized object could not be found
IOException:
if an I/O error occurs
HeadlessException:
if GraphicsEnvironment.isHeadless() returns true
Since
1.4
See Also
java.awt.GraphicsEnvironment#isHeadless
setAlignmentback to summary
public synchronized 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:int

the alignment to be set.

Exceptions
IllegalArgumentException:
if an improper value for alignment is given.
See Also
java.awt.Label#getAlignment
setTextback to summary
public void setText(String text)

Sets the text for this label to the specified text.

Parameters
text:String

the text that this label displays. If text is null, it is treated for display purposes like an empty string "".

See Also
java.awt.Label#getText
java.awt back to summary

protected Class Label.AccessibleAWTLabel

extends AccessibleAWTComponent
Class Inheritance

This class implements accessibility support for the Label class. It provides an implementation of the Java Accessibility API appropriate to label user-interface elements.
Since
1.3

Field Summary

Modifier and TypeField and Description
private static final long
serialVersionUID

Hides java.awt.Component.AccessibleAWTComponent.serialVersionUID.

Use serialVersionUID from JDK 1.3 for interoperability.
Inherited from java.awt.Component.AccessibleAWTComponent:
accessibleAWTComponentHandleraccessibleAWTFocusHandler

Constructor Summary

AccessConstructor and Description
public
AccessibleAWTLabel()

Constructor for the accessible label.

Method Summary

Modifier and TypeMethod and Description
public String

Returns:

the localized name of the object -- can be null if this object does not have a name
getAccessibleName
()

Overrides java.awt.Component.AccessibleAWTComponent.getAccessibleName.

Get the accessible name of this object.

public AccessibleRole

Returns:

an instance of AccessibleRole describing the role of the object
getAccessibleRole
()

Overrides java.awt.Component.AccessibleAWTComponent.getAccessibleRole.

Get the role of this object.

Inherited from java.awt.Component.AccessibleAWTComponent:
addFocusListeneraddPropertyChangeListenercontainsgetAccessibleAtgetAccessibleChildgetAccessibleChildrenCountgetAccessibleComponentgetAccessibleDescriptiongetAccessibleIndexInParentgetAccessibleParentgetAccessibleStateSetgetBackgroundgetBoundsgetCursorgetFontgetFontMetricsgetForegroundgetLocalegetLocationgetLocationOnScreengetSizeisEnabledisFocusTraversableisShowingisVisibleremoveFocusListenerremovePropertyChangeListenerrequestFocussetBackgroundsetBoundssetCursorsetEnabledsetFontsetForegroundsetLocationsetSizesetVisible

Field Detail

serialVersionUIDback to summary
private static final long serialVersionUID

Hides java.awt.Component.AccessibleAWTComponent.serialVersionUID.

Use serialVersionUID from JDK 1.3 for interoperability.

Annotations
@Serial

Constructor Detail

AccessibleAWTLabelback to summary
public AccessibleAWTLabel()

Constructor for the accessible label.

Method Detail

getAccessibleNameback to summary
public String getAccessibleName()

Overrides java.awt.Component.AccessibleAWTComponent.getAccessibleName.

Get the accessible name of this object.

Returns:String

the localized name of the object -- can be null if this object does not have a name

See Also
AccessibleContext#setAccessibleName
getAccessibleRoleback to summary
public AccessibleRole getAccessibleRole()

Overrides java.awt.Component.AccessibleAWTComponent.getAccessibleRole.

Get the role of this object.

Returns:AccessibleRole

an instance of AccessibleRole describing the role of the object

See Also
AccessibleRole