Download the JDK
Search the Tutorials
Hide the TOC
Trail: Creating a GUI with JFC/Swing
Lesson: Using Swing Components
Section: Using Text Components
Using Swing Components
A Visual Index to the Swing Components
Using HTML in Swing Components
Using Top-Level Containers
Using Models
The JComponent Class
Using Text Components
Text Component Features
The Text Component API
How to Use Various Components
How to Make Applets
How to Use Buttons, Check Boxes, and Radio Buttons
How to Use Color Choosers
How to Use Combo Boxes
How to Make Dialogs
How to Use Editor Panes and Text Panes
How to Use File Choosers
How to Use Formatted Text Fields
How to Make Frames (Main Windows)
How to Use Internal Frames
How to Use Labels
How to Use Layered Panes
How to Use Lists
How to Use Menus
How to Use Panels
How to Use Password Fields
How to Use Progress Bars
How to Use Root Panes
How to Use Scroll Panes
How to Use Separators
How to Use Sliders
How to Use Spinners
How to Use Split Panes
How to Use Tabbed Panes
How to Use Tables
How to Use Text Areas
How to Use Text Fields
How to Use Tool Bars
How to Use Tool Tips
How to Use Trees
How to Use Icons
How to Use Borders
Solving Common Component Problems
Home Page > Creating a GUI with JFC/Swing > Using Swing Components
« Previous • Trail • Next »
The Text Component API

> This section lists commonly used parts of the API shared by text components, much of it defined by the `JTextComponent` class. Text Component Features discusses how to use some of this API.

> Also see The JComponent Class, which describes the API that text components inherit from `JComponent`. For information about the API for specific text components, see the how-to page for the component: text field, password field, formatted text field, text area, or editor pane and text pane.

> For a complete listing of and further details about the text API, see the API documentation for `JTextComponent` and for the various classes and interfaces in the text package.

> The API listed in this section falls into these categories:

> - Setting Attributes
> - Manipulating the Selection
> - Converting Positions Between the Model and The View
> - Text Editing Commands
> - Classes and Interfaces that Represent Documents
> - Working with Documents
> - Manipulating Carets and Selection Highlighters
> - Reading and Writing Text

> Setting Attributes
> These methods are in the `JTextComponent` class.
> Method | Description
> --- | ---
> void setDisabledTextColor(Color) Color getDisabledTextColor() | Set or get the color used to display text when the text component is disabled.
> 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.
> void setDragEnabled(boolean) boolean getDragEnabled() | Set or get the `dragEnabled` property, which must be true to enable drag handling on this component. The default value is false. See Drag and Drop for more details.

> Manipulating the Selection
> These methods are in the `JTextComponent` class.
> Method | Description
> --- | ---
> 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.

> Converting Positions Between the Model and the View
> These methods are in the `JTextComponent` class.
> Method | Description
> --- | ---
> int viewToModel(Point) | Convert the specified point in the view coordinate system to a position within the text.
> Rectangle modelToView(int) | Convert the specified position within the text to a rectangle in the view coordinate system.

> Text Editing Commands
> Class or Method | Description
> --- | ---
> void cut() void copy() void paste() void replaceSelection(String) (in `JTextComponent`) | Cut, copy, and paste text using the system clipboard.
> EditorKit | Provides a text component's view factory, document, caret, and actions, as well as reading and writing documents of a particular format.
> DefaultEditorKit | A concrete subclass of `EditorKit` that provides the basic text editing capabilities.
> StyledEditorKit | 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. See Associating Text Actions with Menus and Buttons.
> BeepAction CopyAction CutAction DefaultKeyTypedAction InsertBreakAction InsertContentAction InsertTabAction PasteAction (in `DefaultEditorKit`) | Inner classes that implement various text editing commands.
> AlignmentAction BoldAction FontFamilyAction FontSizeAction ForegroundAction ItalicAction StyledTextAction UnderlineAction (in `StyledEditorKit`) | 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.
> InputMap getInputMap() (in `JComponent`) | Gets the input map that binds key strokes to actions. See Associating Text Actions with Key Strokes.
> void put(KeyStroke, Object) (in `InputMap`) | Binds the specified key to the specified action. You generally specify the action by its name, which for standard editing actions is represented by a string constant such as `DefaultEditorKit.backwardAction`.

> Classes and Interfaces that Represent Documents
> Interface or Class | Description
> --- | ---
> Document | An interface that defines the API that must be implemented by all documents.
> AbstractDocument | An abstract superclass implementation of the `Document` interface. This is the superclass for all documents provided by the Swing text package.
> PlainDocument | A class that implements the `Document` interface. This is 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 | A `Document` subinterface. Defines the API that must be implemented by documents that support styled text. `JTextPane` requires that its document be of this type.
> DefaultStyledDocument | A class that implements the `StyledDocument` interface. The default document for `JTextPane`.

> Working with Documents
> Class or Method | Description
> --- | ---
> DocumentFilter | The superclass of all document filters. You can use a document filter to change what gets inserted or removed from a document, without having to implement a document yourself. See Implementing a Document Filter.
> void setDocumentFilter(DocumentFilter) (in `AbstractDocument`) | Set the document filter.
> | void setDocument(Document) Document getDocument() (in `JTextComponent`) | Set or get the document for a text component.
> Document createDefaultModel() (in `JTextField`) | Override this method to create a custom document instead of the default `PlainDocument`.
> 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`) void setDocumentProperties(Dictionary) Dictionary getDocumentProperties() (in `AbstractDocument`) | A `Document` maintains a set of properties that you can manipulate with these methods.

> Manipulating Carets and Selection Highlighters
> The methods in this table are defined in the `JTextComponent` class.
> Interface, Class, or Method | Description
> --- | ---
> Caret | An interface that defines the API for objects that represent an insertion point within documents.
> DefaultCaret | The default caret used by all text components.
> void setCaret(Caret) Caret getCaret() | Set or get the caret object used by a text component.
> void setCaretColor(Color) Color getCaretColor() | Set or get the color of the caret.
> void setCaretPosition(int) void moveCaretPosition(int) int getCaretPosition() | Set or get the current position of the caret within the document.
> void addCaretListener(CaretListener) void removeCaretListener(CaretListener) | Add or remove a caret listener to a text component.
> NavigationFilter | The superclass for all navigation filters. A navigation filter lets you modify caret changes that are about to occur for a text component.
> void setNavigationFilter(NavigationFilter) | Attach a navigation filter to a text component.
> Highlighter | An interface that defines the API for objects used to highlight the current selection.
> DefaultHighlighter | The default highlighter used by all text components.
> void setHighlighter(Highlighter) Highlighter getHighlighter() | Set or get the highlighter used by a text component.

> Reading and Writing Text
> Method | Description
> --- | ---
> void read(Reader, Object) void 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.

« Previous • Trail • Next »
Previous page: Text Component Features
Next page: How to Use Various Components