Previous | Next | Trail Map | Creating a GUI with JFC/Swing | Using Swing Components

Summary of Text

Swing's text API is large. This tutorial has shown you a simple example of using each component, covered the foundation laid by JTextComponent, and shown you how to utilize that foundation to do interesting things with text components. If your appetite for text is still strong, please refer to our book.html file for references to further documentation. Additionally we provide API tables for the text API and a list of examples that use text components.

The Text API

This section provides these text component API tables:

Swing Text Component Classes
ClassDescription
JTextComponent The abstract superclass of all Swing text components.
JTextField An optionally editable, single-line, plain text component. See How to Use Text Fields.
JPasswordField An optionally editable, single-line, plain text component that masks its content. See Providing a Password Field.
JTextArea An optionally editable, multi-line, plain text component.
JEditorPane An optionally editable, multi-line, styled text component.
JTextPane An optionally editable, multi-line, styled text component with support for named attributes.

JTextComponent Methods for Setting Attributes
MethodDescription
void setDisabledTextColor(Color)
Color getDisabledTextColor()
Set or get the color used to display text when the text component is disabled.
void setOpaque(boolean)
boolean getOpaque()
Set or get whether the text component is completely opaque.
void setMargin(Insets)
Insets getMargin()
Set or get the margin between the text and the text component's border.
void setEditable(boolean)
boolean isEditable()
Set or get whether the user can edit the text in the text component.

Converting Positions Between the Model and the View
MethodDescription
int viewToModel(Point)
(in JTextComponent)
Convert the specified point in the view coordinate system to a position within the text.
Rectangle modelToView(int)
(in JTextComponent)
Convert the specified position within the text to a rectangle in the view coordinate system.

Classes and Interfaces that Represent Documents
Interface or ClassDescription
Document
(an interface)
Defines the API that must be implemented by all documents.
AbstractDocument
(an interface)
An abstract superclass implementation of the Document interface. This is the superclass for all documents provided by the Swing text package.
PlainDocument
(a class)
Implements the Document interface. The default document for the plain text components (text field, password field, and text area). Additionally used by editor pane and text pane when loading plain text or text of an unknown format.
StyledDocument
(an interface)
A Document subinterface. Defines the API that must be implemented by documents that support styled text.
DefaultStyledDocument
(a class)
Implements the StyledDocument interface. The default document for the styled text components (editor pane and text pane).

Useful Methods for Working with Documents
MethodDescription
setDocument(Document)
Document getDocument()

(in JTextComponent
Set or get the document for a text component.
Document createDefaultModel()
(in JTextField
Override this method in text field and its subclasses to create a custom document instead of the default PlainDocument. Creating a Validated Field provides an example of overriding this method.
void insertString(int, String, AttributeSet)
void remove(int, int)

(in Document)
These methods are commonly overridden by custom documents. For an example of a custom document that overrides both of these methods, see Creating a Validated Field.
void addDocumentListener(DocumentListener)
void removeDocumentListener(DocumentListener)

(in Document)
Add or remove a document listener to a document. See Listening for Changes on a Document.
void addUndoableEditListener(UndoableEditListener)
void removeUndoableEditListener(UndoableEditlistener)

(in Document)
Add or remove an undoable edit listener to a document. Undoable edit listeners are used in Implementing Undo and Redo.
int getLength()
Position getStartPosition()
Position getEndPosition()
String getText(int, int)

(in Document)
Document methods that return useful information about the document.
Object getProperty(Object)
void putProperty(Object, Object)

(in Document)
Dictionary getDocumentProperties()
void setDocumentProperties(Dictionary)

(in AbstractDocument)
A Document maintains a set of properties that you can manipulate with these methods. The example described in Using a Document Listener on a Text Field uses a property to name text components so that a shared document listener can identify the document from whence came an event.

JTextComponent Methods for Manipulating the Current Selection
MethodDescription
String getSelectedText() Get the currently selected text.
void selectAll()
void select(int, int)
Select all text or select text within a start and end range.
void setSelectionStart(int)
void setSelectionEnd(int)
int getSelectionStart()
int getSelectionEnd()
Set or get extent of the current selection by index.
void setSelectedTextColor(Color)
Color getSelectedTextColor()
Set or get the color of selected text.
void setSelectionColor(Color)
Color getSelectionColor()
Set or get the background color of selected text.

Manipulating Carets and Selection Highlighters
Interface, Class, or MethodDescription
Caret
(an interface)
Defines the API for objects that represent an insertion point within documents.
DefaultCaret
(a class)
The default caret used by all text components.
void setCaret(Caret)
Caret getCaret()

(in JTextComponent)
Set or get the caret object used by a text component.
void setCaretColor(Color)
Color getCaretColor()

(in JTextComponent)
Set or get the color of the caret.
void setCaretPosition(Position)
void moveCaretPosition(int)
Position getCaretPosition()

(in JTextComponent)
Set or get the current position of the caret within the document.
void addCaretListener(CaretListener)
void removeCaretListener(CaretListener)

(in JTextComponent)
Add or remove a caret listener to a text component.
Highlighter
(an interface)
Defines the API for objects used to highlight the current selection.
DefaultHighlighter
(a class)
The default highlighter used by all text components.
void setHighlighter(Highlighter)
Highlighter getHighlighter()

(in JTextComponent)
Set or get the highlighter used by a text component.

Text Editing Commands
Class or MethodDescription
void cut()
void copy()
void paste()
void replaceSelection(String)

(in JTextComponent)
Cut, copy, and paste text using the system clipboard.
EditorKit
(a class)
Edit, read, and write text of a particular format.
DefaultEditorKit
(a class)
A concrete subclass of EditorKit that provides the basic text editing capabilities.
StyledEditorKit
(a class)
A subclass of Default EditorKit that provides additional editing capabilities for styled text.
String xxxxAction
(in DefaultEditorKit)
The names of all the actions supported by the default editor kit.
BeepAction
CopyAction
CutAction
DefaultKeyTypedAction
InsertBreakAction
InsertContentAction
InsertTabAction
PasteAction

(in DefaultEditorKit)
A collection inner classes that implement various text editing commands.
AlignmentAction
BoldAction
FontFamilyAction
FontSizeAction
ForegroundAction
ItalicAction
StyledTextAction
UnderlineAction

(in StyledEditorKit)
A collection inner classes that implement various editing commands for styled text.
Action[] getActions()
(in JTextComponent)
Get the actions supported by this component. This method gets the array of actions from the editor kit if one is used by the component.

Binding KeyStrokes to Actions
Interface or MethodDescription
Keymap
(an interface)
An interface for managing a set of key bindings. A key binding is represented by a keystroke/action pair.
Keymap addKeymap(nm, Keymap)
Keymap removeKeymap(nm)
Keymap getKeymap(nm)

(in JTextComponent)
Add or remove a keymap to the keymap hierarchy. Also get a keymap by name. Note that these are class methods. The keymap hierarchy is shared by all text components.
void loadKeymap(Keymap, KeyBinding[], Action[])
(in JTextComponent)
Adds a set of key bindings to the specified keymap. This is a class method.
void setKeymap(Keymap)
Keymap getKeymap()

(in JTextComponent)
Set or get the currently active keymap for a particular text component.
void addActionForKeyStroke(KeyStroke, Action)
Action getAction(KeyStroke)
KeyStroke[] getKeyStrokesForAction(Action)

(in Keymap)
Set or get keystroke/action binding from a keymap.
boolean isLocallyDefined(KeyStroke)
(in Keymap)
Get whether the specified keystroke is bound to an action in a keymap.
void removeKeyStrokeBinding(KeyStroke)
void removeBindings()

(in Keymap)
Remove one or all key bindings from a keymap.
void setDefaultAction(Action)
Action getDefaultAction()

(in Keymap)
Set or get the default action. This action is fired if a keystroke is not explicitly bound to an action.
Action[] getBoundActions()
KeyStroke[] getBoundKeyStrokes()

(in Keymap)
Get an array containing all of the bound actions or keystrokes in a keymap.

Reading and Writing Text
MethodDescription
void JTextComponent.read(Reader, Object)
void JTextComponent.write(Writer)

(in JTextComponent)
Read or write text.
void read(Reader, Document, int)
void read(InputStream, Document, int)

(in EditorKit)
Read text from a stream into a document.
void write(Writer, Document, int, int)
void write(OutputStream, Document, int, int)

(in EditorKit)
Write text from a document to a stream.

API for Displaying Text from a URL
Method or ConstructorDescription
JEditorPane(URL)
JEditorPane(String)

(in JEditorPane)
Create an editor pane loaded with the text at the specified URL.
setPage(URL)
setPage(String)

(in JEditorPane)
Load an editor pane (or text pane) with the text at the specified URL.
URL getPage()
(in JEditorPane)
Get the URL for the editor pane's (or text pane's) current page.

Examples that Use Text Components

This table shows the examples that use text components and where those examples are described. To get started with text, you might want to run these programs and examine their code to find one that does something similar to what you want to do. [PENDING: update list to include text sampler and others]

Example Where Described Notes
TextSamplerDemo.java This page and
Using Swing's Text Components
Uses one of each of Swing's text components.
TextDemo.java How to Use Text Fields Uses a basic text field and a basic text area.
TextComponentDemo.java This page Provides a customized text pane. Illustrates many text component features.
TextFieldDemo.java How to Use Text Fields Implements two different keystroke-validated text fields.
PasswordDemo.java How to Use Text Fields and
Using the SwingWorker Class
Uses a password field.
ToolBarDemo2.java How to Use Tool Bars Puts a text field in a tool bar.
CustomDialog.java How to Use Dialogs Puts a validated text field in a dialog. Part of DialogDemo (under the More Dialogs tab).
TreeDemo.java How to Use Trees Uses an editor pane to display help loaded from an HTML file.
the stylepad and note pad examples that ship with swing    


Previous | Next | Trail Map | Creating a GUI with JFC/Swing | Using Swing Components