With the JLabel
class, you can display unselectable text and images. If you need to create a component that displays a string, an image, or both, you can do so by using or extending JLabel
. If the component is interactive and has a certain state, use a button instead of a label.
By specifying HTML code in a label's text, you can give the label various characteristics such as multiple lines, multiple fonts or multiple colors. If the label uses just a single color or font, you can avoid the overhead of HTML processing by using the setForeground
or setFont
method instead. See Using HTML in Swing Components for details.
Note that labels are not opaque by default. If you need to paint the label's background, it is recommended that you turn its opacity property to "true". The following code snippet shows how to do this.
The following picture introduces 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:Below is the code from LabelDemo.java
that creates the labels in the previous example.
ImageIcon icon = createImageIcon("images/middle.gif"); . . . label1 = new JLabel("Image and Text", icon, JLabel.CENTER); //Set the position of the text, relative to the icon: label1.setVerticalTextPosition(JLabel.BOTTOM); label1.setHorizontalTextPosition(JLabel.CENTER); label2 = new JLabel("Text-Only Label"); label3 = new JLabel(icon);
The code for the createImageIcon
method is similar to that used throughout this tutorial. You can find it in How to Use Icons.
Often, a label describes another component. When this occurs, you can improve your program's accessibility by using the setLabelFor
method to identify the component that the label describes. For example:
amountLabel.setLabelFor(amountField);
The preceding code, taken from the FormattedTextFieldDemo
example discussed in How to Use Formatted Text Fields, lets assistive technologies know that the label (amountLabel
) provides information about the formatted text field (amountField
). For more information about assistive technologies, see How to Support Assistive Technologies.
The following tables list the commonly used JLabel
constructors and methods. Other methods you are likely to call are defined by the Component
and JComponent
classes. They include setFont
, setForeground
, setBorder
, setOpaque
, and setBackground
. See The JComponent Class for details. The API for using labels falls into three categories:
In the following API, do not confuse label alignment with X and Y alignment. X and Y alignment are used by layout managers and can affect the way any component — not just a label — is sized or positioned. Label alignment, on the other hand, has no effect on a label's size or position. Label alignment simply determines where, inside the label's painting area, the label's contents are positioned. Typically, the label's painting area is exactly the size needed to paint on the label and thus label alignment is irrelevant. For more information about X and Y alignment, see How to Use BoxLayout.
Setting or Getting the Label's Contents Method or Constructor Purpose JLabel(Icon)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 SwingConstants
interface (which JLabel
implements): LEFT
, CENTER
, RIGHT
, LEADING
, or TRAILING
. For ease of localization, we strongly recommend using LEADING
and TRAILING
, rather than LEFT
and RIGHT
. void setText(String)
setLabelFor
), then when the user activates the mnemonic, the keyboard focus is transferred to the component specified by the labelFor property. void setDisplayedMnemonicIndex(int)
setDisplayedMnemonicIndex(5)
decorates the character that is at position 5 (that is, the 6th character in the text). Not all types of look and feel may support this feature. void setDisabledIcon(Icon)
SwingConstants
interface defines five possible values for horizontal alignment: LEFT
, CENTER
(the default for image-only labels), RIGHT
, LEADING
(the default for text-only labels), TRAILING
. For vertical alignment: TOP
, CENTER
(the default), and BOTTOM
. void setHorizontalTextPosition(int)
SwingConstants
interface defines five possible values for horizontal position: LEADING
, LEFT
, CENTER
, RIGHT
, and TRAILING
(the default). For vertical position: TOP
, CENTER
(the default), and BOTTOM
. void setIconTextGap(int)
The following table lists some of the many examples that use labels.
See the Using JavaFX UI Controls: Label tutorial to learn about JavaFX labeled controls.
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4