Constructor: |
public JLabel() {
this("", null, LEADING);
}
Creates a JLabel instance with
no image and with an empty string for the title.
The label is centered vertically
in its display area.
The label's contents, once set, will be displayed on the leading edge
of the label's display area. |
public JLabel(String text) {
this(text, null, LEADING);
}
Creates a JLabel instance with the specified text.
The label is aligned against the leading edge of its display area,
and centered vertically. Parameters:
text - The text to be displayed by the label.
|
public JLabel(Icon image) {
this(null, image, CENTER);
}
Creates a JLabel instance with the specified image.
The label is centered vertically and horizontally
in its display area. Parameters:
image - The image to be displayed by the label.
|
public JLabel(String text,
int horizontalAlignment) {
this(text, null, horizontalAlignment);
}
Creates a JLabel instance with the specified
text and horizontal alignment.
The label is centered vertically in its display area. Parameters:
text - The text to be displayed by the label.
horizontalAlignment - One of the following constants
defined in SwingConstants :
LEFT ,
CENTER ,
RIGHT ,
LEADING or
TRAILING .
|
public JLabel(Icon image,
int horizontalAlignment) {
this(null, image, horizontalAlignment);
}
Creates a JLabel instance with the specified
image and horizontal alignment.
The label is centered vertically in its display area. Parameters:
image - The image to be displayed by the label.
horizontalAlignment - One of the following constants
defined in SwingConstants :
LEFT ,
CENTER ,
RIGHT ,
LEADING or
TRAILING .
|
public JLabel(String text,
Icon icon,
int horizontalAlignment) {
setText(text);
setIcon(icon);
setHorizontalAlignment(horizontalAlignment);
updateUI();
setAlignmentX(LEFT_ALIGNMENT);
}
Creates a JLabel instance with the specified
text, image, and horizontal alignment.
The label is centered vertically in its display area.
The text is on the trailing edge of the image. Parameters:
text - The text to be displayed by the label.
icon - The image to be displayed by the label.
horizontalAlignment - One of the following constants
defined in SwingConstants :
LEFT ,
CENTER ,
RIGHT ,
LEADING or
TRAILING .
|
Method from javax.swing.JLabel Detail: |
protected int checkHorizontalKey(int key,
String message) {
if ((key == LEFT) ||
(key == CENTER) ||
(key == RIGHT) ||
(key == LEADING) ||
(key == TRAILING)) {
return key;
}
else {
throw new IllegalArgumentException(message);
}
}
Verify that key is a legal value for the horizontalAlignment properties. |
protected int checkVerticalKey(int key,
String message) {
if ((key == TOP) || (key == CENTER) || (key == BOTTOM)) {
return key;
}
else {
throw new IllegalArgumentException(message);
}
}
Verify that key is a legal value for the
verticalAlignment or verticalTextPosition properties. |
public AccessibleContext getAccessibleContext() {
if (accessibleContext == null) {
accessibleContext = new AccessibleJLabel();
}
return accessibleContext;
}
Get the AccessibleContext of this object |
public Icon getDisabledIcon() {
if (!disabledIconSet && disabledIcon == null && defaultIcon != null) {
disabledIcon = UIManager.getLookAndFeel().getDisabledIcon(this, defaultIcon);
if (disabledIcon != null) {
firePropertyChange("disabledIcon", null, disabledIcon);
}
}
return disabledIcon;
}
Returns the icon used by the label when it's disabled.
If no disabled icon has been set this will forward the call to
the look and feel to construct an appropriate disabled Icon.
Some look and feels might not render the disabled Icon, in which
case they will ignore this. |
public int getDisplayedMnemonic() {
return mnemonic;
}
Return the keycode that indicates a mnemonic key.
This property is used when the label is part of a larger component.
If the labelFor property of the label is not null, the label will
call the requestFocus method of the component specified by the
labelFor property when the mnemonic is activated. |
public int getDisplayedMnemonicIndex() {
return mnemonicIndex;
}
Returns the character, as an index, that the look and feel should
provide decoration for as representing the mnemonic character. |
public int getHorizontalAlignment() {
return horizontalAlignment;
}
Returns the alignment of the label's contents along the X axis. |
public int getHorizontalTextPosition() {
return horizontalTextPosition;
}
Returns the horizontal position of the label's text,
relative to its image. |
public Icon getIcon() {
return defaultIcon;
}
Returns the graphic image (glyph, icon) that the label displays. |
public int getIconTextGap() {
return iconTextGap;
}
Returns the amount of space between the text and the icon
displayed in this label. |
public Component getLabelFor() {
return labelFor;
}
Get the component this is labelling. |
public String getText() {
return text;
}
Returns the text string that the label displays. |
public LabelUI getUI() {
return (LabelUI)ui;
}
Returns the L&F object that renders this component. |
public String getUIClassID() {
return uiClassID;
}
Returns a string that specifies the name of the l&f class
that renders this component. |
public int getVerticalAlignment() {
return verticalAlignment;
}
Returns the alignment of the label's contents along the Y axis. |
public int getVerticalTextPosition() {
return verticalTextPosition;
}
Returns the vertical position of the label's text,
relative to its image. |
public boolean imageUpdate(Image img,
int infoflags,
int x,
int y,
int w,
int h) {
// Don't use getDisabledIcon, will trigger creation of icon if icon
// not set.
if (!isShowing() ||
!SwingUtilities.doesIconReferenceImage(getIcon(), img) &&
!SwingUtilities.doesIconReferenceImage(disabledIcon, img)) {
return false;
}
return super.imageUpdate(img, infoflags, x, y, w, h);
}
This is overridden to return false if the current Icon's Image is
not equal to the passed in Image img . |
protected String paramString() {
String textString = (text != null ?
text : "");
String defaultIconString = ((defaultIcon != null)
&& (defaultIcon != this) ?
defaultIcon.toString() : "");
String disabledIconString = ((disabledIcon != null)
&& (disabledIcon != this) ?
disabledIcon.toString() : "");
String labelForString = (labelFor != null ?
labelFor.toString() : "");
String verticalAlignmentString;
if (verticalAlignment == TOP) {
verticalAlignmentString = "TOP";
} else if (verticalAlignment == CENTER) {
verticalAlignmentString = "CENTER";
} else if (verticalAlignment == BOTTOM) {
verticalAlignmentString = "BOTTOM";
} else verticalAlignmentString = "";
String horizontalAlignmentString;
if (horizontalAlignment == LEFT) {
horizontalAlignmentString = "LEFT";
} else if (horizontalAlignment == CENTER) {
horizontalAlignmentString = "CENTER";
} else if (horizontalAlignment == RIGHT) {
horizontalAlignmentString = "RIGHT";
} else if (horizontalAlignment == LEADING) {
horizontalAlignmentString = "LEADING";
} else if (horizontalAlignment == TRAILING) {
horizontalAlignmentString = "TRAILING";
} else horizontalAlignmentString = "";
String verticalTextPositionString;
if (verticalTextPosition == TOP) {
verticalTextPositionString = "TOP";
} else if (verticalTextPosition == CENTER) {
verticalTextPositionString = "CENTER";
} else if (verticalTextPosition == BOTTOM) {
verticalTextPositionString = "BOTTOM";
} else verticalTextPositionString = "";
String horizontalTextPositionString;
if (horizontalTextPosition == LEFT) {
horizontalTextPositionString = "LEFT";
} else if (horizontalTextPosition == CENTER) {
horizontalTextPositionString = "CENTER";
} else if (horizontalTextPosition == RIGHT) {
horizontalTextPositionString = "RIGHT";
} else if (horizontalTextPosition == LEADING) {
horizontalTextPositionString = "LEADING";
} else if (horizontalTextPosition == TRAILING) {
horizontalTextPositionString = "TRAILING";
} else horizontalTextPositionString = "";
return super.paramString() +
",defaultIcon=" + defaultIconString +
",disabledIcon=" + disabledIconString +
",horizontalAlignment=" + horizontalAlignmentString +
",horizontalTextPosition=" + horizontalTextPositionString +
",iconTextGap=" + iconTextGap +
",labelFor=" + labelForString +
",text=" + textString +
",verticalAlignment=" + verticalAlignmentString +
",verticalTextPosition=" + verticalTextPositionString;
}
Returns a string representation of this JLabel. 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 . |
public void setDisabledIcon(Icon disabledIcon) {
Icon oldValue = this.disabledIcon;
this.disabledIcon = disabledIcon;
disabledIconSet = (disabledIcon != null);
firePropertyChange("disabledIcon", oldValue, disabledIcon);
if (disabledIcon != oldValue) {
if (disabledIcon == null || oldValue == null ||
disabledIcon.getIconWidth() != oldValue.getIconWidth() ||
disabledIcon.getIconHeight() != oldValue.getIconHeight()) {
revalidate();
}
if (!isEnabled()) {
repaint();
}
}
}
|
public void setDisplayedMnemonic(int key) {
int oldKey = mnemonic;
mnemonic = key;
firePropertyChange("displayedMnemonic", oldKey, mnemonic);
setDisplayedMnemonicIndex(
SwingUtilities.findDisplayedMnemonicIndex(getText(), mnemonic));
if (key != oldKey) {
revalidate();
repaint();
}
}
Specify a keycode that indicates a mnemonic key.
This property is used when the label is part of a larger component.
If the labelFor property of the label is not null, the label will
call the requestFocus method of the component specified by the
labelFor property when the mnemonic is activated. |
public void setDisplayedMnemonic(char aChar) {
int vk = java.awt.event.KeyEvent.getExtendedKeyCodeForChar(aChar);
if (vk != java.awt.event.KeyEvent.VK_UNDEFINED) {
setDisplayedMnemonic(vk);
}
}
Specifies the displayedMnemonic as a char value. |
public void setDisplayedMnemonicIndex(int index) throws IllegalArgumentException {
int oldValue = mnemonicIndex;
if (index == -1) {
mnemonicIndex = -1;
} else {
String text = getText();
int textLength = (text == null) ? 0 : text.length();
if (index < -1 || index >= textLength) { // index out of range
throw new IllegalArgumentException("index == " + index);
}
}
mnemonicIndex = index;
firePropertyChange("displayedMnemonicIndex", oldValue, index);
if (index != oldValue) {
revalidate();
repaint();
}
}
Provides a hint to the look and feel as to which character in the
text should be decorated to represent the mnemonic. Not all look and
feels may support this. A value of -1 indicates either there is no
mnemonic, the mnemonic character is not contained in the string, or
the developer does not wish the mnemonic to be displayed.
The value of this is updated as the properties relating to the
mnemonic change (such as the mnemonic itself, the text...).
You should only ever have to call this if
you do not wish the default character to be underlined. For example, if
the text was 'Save As', with a mnemonic of 'a', and you wanted the 'A'
to be decorated, as 'Save As', you would have to invoke
setDisplayedMnemonicIndex(5) after invoking
setDisplayedMnemonic(KeyEvent.VK_A) . |
public void setHorizontalAlignment(int alignment) {
if (alignment == horizontalAlignment) return;
int oldValue = horizontalAlignment;
horizontalAlignment = checkHorizontalKey(alignment,
"horizontalAlignment");
firePropertyChange("horizontalAlignment",
oldValue, horizontalAlignment);
repaint();
}
|
public void setHorizontalTextPosition(int textPosition) {
int old = horizontalTextPosition;
this.horizontalTextPosition = checkHorizontalKey(textPosition,
"horizontalTextPosition");
firePropertyChange("horizontalTextPosition",
old, horizontalTextPosition);
revalidate();
repaint();
}
Sets the horizontal position of the label's text,
relative to its image. |
public void setIcon(Icon icon) {
Icon oldValue = defaultIcon;
defaultIcon = icon;
/* If the default icon has really changed and we had
* generated the disabled icon for this component
* (in other words, setDisabledIcon() was never called), then
* clear the disabledIcon field.
*/
if ((defaultIcon != oldValue) && !disabledIconSet) {
disabledIcon = null;
}
firePropertyChange("icon", oldValue, defaultIcon);
if ((accessibleContext != null) && (oldValue != defaultIcon)) {
accessibleContext.firePropertyChange(
AccessibleContext.ACCESSIBLE_VISIBLE_DATA_PROPERTY,
oldValue, defaultIcon);
}
/* If the default icon has changed and the new one is
* a different size, then revalidate. Repaint if the
* default icon has changed.
*/
if (defaultIcon != oldValue) {
if ((defaultIcon == null) ||
(oldValue == null) ||
(defaultIcon.getIconWidth() != oldValue.getIconWidth()) ||
(defaultIcon.getIconHeight() != oldValue.getIconHeight())) {
revalidate();
}
repaint();
}
}
Defines the icon this component will display. If
the value of icon is null, nothing is displayed.
The default value of this property is null.
This is a JavaBeans bound property. |
public void setIconTextGap(int iconTextGap) {
int oldValue = this.iconTextGap;
this.iconTextGap = iconTextGap;
firePropertyChange("iconTextGap", oldValue, iconTextGap);
if (iconTextGap != oldValue) {
revalidate();
repaint();
}
}
If both the icon and text properties are set, this property
defines the space between them.
The default value of this property is 4 pixels.
This is a JavaBeans bound property. |
public void setLabelFor(Component c) {
Component oldC = labelFor;
labelFor = c;
firePropertyChange("labelFor", oldC, c);
if (oldC instanceof JComponent) {
((JComponent)oldC).putClientProperty(LABELED_BY_PROPERTY, null);
}
if (c instanceof JComponent) {
((JComponent)c).putClientProperty(LABELED_BY_PROPERTY, this);
}
}
Set the component this is labelling. Can be null if this does not
label a Component. If the displayedMnemonic property is set
and the labelFor property is also set, the label will
call the requestFocus method of the component specified by the
labelFor property when the mnemonic is activated. |
public void setText(String text) {
String oldAccessibleName = null;
if (accessibleContext != null) {
oldAccessibleName = accessibleContext.getAccessibleName();
}
String oldValue = this.text;
this.text = text;
firePropertyChange("text", oldValue, text);
setDisplayedMnemonicIndex(
SwingUtilities.findDisplayedMnemonicIndex(
text, getDisplayedMnemonic()));
if ((accessibleContext != null)
&& (accessibleContext.getAccessibleName() != oldAccessibleName)) {
accessibleContext.firePropertyChange(
AccessibleContext.ACCESSIBLE_VISIBLE_DATA_PROPERTY,
oldAccessibleName,
accessibleContext.getAccessibleName());
}
if (text == null || oldValue == null || !text.equals(oldValue)) {
revalidate();
repaint();
}
}
Defines the single line of text this component will display. If
the value of text is null or empty string, nothing is displayed.
The default value of this property is null.
This is a JavaBeans bound property. |
public void setUI(LabelUI ui) {
super.setUI(ui);
// disabled icon is generated by LF so it should be unset here
if (!disabledIconSet && disabledIcon != null) {
setDisabledIcon(null);
}
}
Sets the L&F object that renders this component. |
public void setVerticalAlignment(int alignment) {
if (alignment == verticalAlignment) return;
int oldValue = verticalAlignment;
verticalAlignment = checkVerticalKey(alignment, "verticalAlignment");
firePropertyChange("verticalAlignment", oldValue, verticalAlignment);
repaint();
}
|
public void setVerticalTextPosition(int textPosition) {
if (textPosition == verticalTextPosition) return;
int old = verticalTextPosition;
verticalTextPosition = checkVerticalKey(textPosition,
"verticalTextPosition");
firePropertyChange("verticalTextPosition", old, verticalTextPosition);
revalidate();
repaint();
}
Sets the vertical position of the label's text,
relative to its image.
The default value of this property is CENTER.
This is a JavaBeans bound property. |
public void updateUI() {
setUI((LabelUI)UIManager.getUI(this));
}
Resets the UI property to a value from the current look and feel. |