Java Reference
In-Depth Information
Swing Text Components
The JTextField, JTextArea, and JPasswordField represent Swing text compo-
nents. The JTextField and JTextArea classes are child classes of JTextCompo-
nent, while JPasswordField is a child class of JTextField.
The methods of JTextComponent are similar to those in TextComponent,
with a notable exception: JTextComponent objects do not generate a TextEvent,
so the addTextListener() is not in JTextComponent. Notice, however, some
interesting methods in JTextComponent, including:
public void addCaretListener(CaretListener c). This is for listening to a
CaretEvent, which occurs when changes are made to the caret.
public void setKeymap(Keymap map). A Keymap binds keystrokes to
actions. A keymap takes the place of a TextListener in AWT.
public void setCaretColor(Color c). This changes the color of the caret.
public void setSelectionColor(Color c).
This sets the color used when
text is selected.
public void write(Writer w).
This writes the text to the specified output
writer.
The constructors of JTextField are identical to those of TextField, with the
addition of:
public JTextField(Document doc). This creates a JTextField with the
given Document. A Document object contains the content of the text
field and allows the content to be separated from its view in the text
component.
Similarly, the JTextArea constructors look similar to those of TextArea, with
the inclusion of a constructor that takes in a Document. The JTextArea class,
however, does not contain a constructor that involves the policies of the scroll
bars. This is because the JTextArea does not handle its own scroll bars as
TextArea does. See the sidebar Working with Scroll Panes to see how to add
scroll bars to a JTextArea.
Search WWH ::




Custom Search