Java Reference
In-Depth Information
Icon icon. Represents the icon of the label. A JLabel can have both text
and an icon.
int horizontalAlignment. Represents where the text and/or icon appear
within the bounds of the label when it is laid out in the container. The
possible values are LEFT, RIGHT, CENTER, LEADING, and TRAILING,
which are static variables in the javax.swing.SwingConstants interface.
For example, the following statement creates a JLabel that will be centered:
JLabel bye = new JLabel(“Goodbye”, SwingConstants.CENTER);
The JTextComponentDemo program in the next section demonstrates using
the JLabel class.
Text Components
There are two types of GUI text components: text fields and text areas. A text
field is a single line of text, while a text area can be any number of lines of text
and often has vertical and horizontal scroll bars for navigating through the
text area. Let's look at both the AWT and Swing versions of the text components.
AWT Text Components
The java.awt.TextField class represents a text field, and the java.awt.TextArea
class represents a text area. They share a common parent class, java.awt.Text-
Component, which contains many useful methods, including:
public void addTextListener(TextListener t). Both the TextField and
TextArea components generate a TextEvent, which occurs when the text
changes.
public String getText(). This returns the current text in the text component.
public void setText(String s). This sets the text of the text component.
public void setEditable(boolean b). When the argument is false, the user
cannot edit the text in the text component. Text components are editable
by default.
public String getSelectedText().
This returns the text that is currently
selected in the text component.
public void setCaretPosition(int position).
This sets the position of the
caret in the text component.
Be sure to browse the documentation and check the other methods of the
class if you need to work with a TextComponent.
Search WWH ::




Custom Search