Using the JFC/Swing Packages |
With theJLabel
class, you can display unselectable text and images. Sometimes it's tempting to subclassJLabel
to create interactive components that display simple a simple string/image. However, labels can't accept the keyboard focus, so they're unacceptable as the basis of interactive components. [CHECK] Alternatives to subclassingJLabel
include subclassingJButton
or putting the label under an invisibleJComponent
that performs event handling. [CHECK]Here's a picture of 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:
- Compile and run the application. The source file is
LabelDemo.java
.
See Getting Started with Swing if you need help.- Resize the window so you can see how the labels align.
All the labels have the default vertical alignment -- they're in the vertical center of their layout space. The top level label, which contains both image and text, is specified to have horizontal center alignment. The second label, which contains just text, has the left alignment that is the default for text-only labels. The third label, which contains just an image, has horizontal center alignment, which is the default for image-only labels.
Below is the code from
LabelDemo.java
that creates the labels in the previous example.The following tables list the commonly usedImageIcon icon = new ImageIcon("middle.gif"); . . . label1 = new JLabel("Image and Text", icon, JLabel.CENTER); label1.setVerticalTextPosition(JLabel.BOTTOM); label1.setHorizontalTextPosition(JLabel.CENTER); label2 = new JLabel("Text-Only Label"); label3 = new JLabel(icon); //Add labels to the JPanel. add(label1); add(label2); add(label3);JLabel
constructors and methods. Other methods you're likely to call are defined by theComponent
class. They includesetFont
andsetForeground
. [link to Component discussion.]The API for using labels falls into two categories:
Setting or Getting the Label's Contents Method or Constructor Purpose Example JLabel(Icon)
JLabel(Icon, int)
JLabel(String)
JLabel(String, Icon, int)
JLabel(String, int)
JLabel()Create a JLabel
instance, initializing it to have the specified text/image/alignment. Theint
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 theSwingConstants
interface (whichJLabel
implements):LEFT
,CENTER
, orRIGHT
.LabelDemo.java
void setText(String)
String getText()Set or get the text displayed by the label. [none yet] void setIcon(Icon)
Icon getIcon()Set or get the image displayed by the label. [none yet] void setRepresentedKeyAccelerator(char)
char getRepresentedKeyAccelerator()Set or get the letter that should look like a keyboard accelerator. This is handy when a label describes a component (such as a text field) that has a keyboard accelerator but can't display it. [none yet] void setDisabledIcon(Icon)
Icon getDisabledIcon()Set or get the image displayed by the label when it's disabled. If you don't specify a disabled image, then the look-and-feel creates one by manipulating the default image. [none yet]
Fine Tuning the Label's Appearance Method Purpose Example void setHorizontalAlignment(int)
void setVerticalAlignment(int)
int getHorizontalAlignment()
int getVerticalAlignment()Set or get where in the label its contents should be placed. The SwingConstants
interface defines three possible values for horizontal alignment:LEFT
(the default for text-only labels),CENTER
(the default for image-only labels), orRIGHT
. For vertical alignment:TOP
,CENTER
(the default), andBOTTOM
.[none yet] void setHorizontalTextPosition(int)
void setVerticalTextPosition(int)
int getHorizontalTextPosition()
int getVerticalTextPosition()Set or get where the button's text should be placed, relative to the button's image. The SwingConstants
interface defines three possible values for horizontal position:LEFT
,CENTER
, andRIGHT
(the default). For vertical position:TOP
,CENTER
(the default), andBOTTOM
.LabelDemo.java
void setIconTextGap(int)
int getIconTextGap()Set or get the number of pixels between the label's text and its image. [none yet]
Using the JFC/Swing Packages |