Using Swing Components |
Swing's text API is large. This tutorial has shown you a simple example of using each component, covered the foundation laid byJTextComponent
, 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
- JTextComponent Methods for Setting Attributes
- Converting Positions Between the Model and The View
- Classes and Interfaces that Represent Documents
- Useful Methods for Working with Documents
- JTextComponent Methods for Manipulating the Current Selection
- Manipulating Carets and Selection Highlighters
- Text Editing Commands
- Binding KeyStrokes to Actions
- Reading and Writing Text
- API for Displaying Text from a URL
Swing Text Component Classes Class Description 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 Method Description 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 Method Description int viewToModel(Point)
(inJTextComponent
)Convert the specified point in the view coordinate system to a position within the text. Rectangle modelToView(int)
(inJTextComponent
)Convert the specified position within the text to a rectangle in the view coordinate system.
Classes and Interfaces that Represent Documents Interface or Class Description 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 Method Description setDocument(Document)
Document getDocument()
(inJTextComponent
Set or get the document for a text component. Document createDefaultModel()
(inJTextField
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)
(inDocument
)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)
(inDocument
)Add or remove a document listener to a document. See Listening for Changes on a Document. void addUndoableEditListener(UndoableEditListener)
void removeUndoableEditListener(UndoableEditlistener)
(inDocument
)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)
(inDocument
)Document
methods that return useful information about the document.Object getProperty(Object)
void putProperty(Object, Object)
(inDocument
)
Dictionary getDocumentProperties()
void setDocumentProperties(Dictionary)
(inAbstractDocument
)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 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.
Manipulating Carets and Selection Highlighters Interface, Class, or Method Description 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()
(inJTextComponent
)Set or get the caret object used by a text component. void setCaretColor(Color)
Color getCaretColor()
(inJTextComponent
)Set or get the color of the caret. void setCaretPosition(Position)
void moveCaretPosition(int)
Position getCaretPosition()
(inJTextComponent
)Set or get the current position of the caret within the document. void addCaretListener(CaretListener)
void removeCaretListener(CaretListener)
(inJTextComponent
)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()
(inJTextComponent
)Set or get the highlighter used by a text component.
Text Editing Commands Class or Method Description void cut()
void copy()
void paste()
void replaceSelection(String)
(inJTextComponent
)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
(inDefaultEditorKit
)The names of all the actions supported by the default editor kit. BeepAction
CopyAction
CutAction
DefaultKeyTypedAction
InsertBreakAction
InsertContentAction
InsertTabAction
PasteAction
(inDefaultEditorKit
)A collection inner classes that implement various text editing commands. AlignmentAction
BoldAction
FontFamilyAction
FontSizeAction
ForegroundAction
ItalicAction
StyledTextAction
UnderlineAction
(inStyledEditorKit
)A collection inner classes that implement various editing commands for styled text. Action[] getActions()
(inJTextComponent
)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 Method Description 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)
(inJTextComponent
)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[])
(inJTextComponent
)Adds a set of key bindings to the specified keymap. This is a class method. void setKeymap(Keymap)
Keymap getKeymap()
(inJTextComponent
)Set or get the currently active keymap for a particular text component. void addActionForKeyStroke(KeyStroke, Action)
Action getAction(KeyStroke)
KeyStroke[] getKeyStrokesForAction(Action)
(inKeymap
)Set or get keystroke/action binding from a keymap. boolean isLocallyDefined(KeyStroke)
(inKeymap
)Get whether the specified keystroke is bound to an action in a keymap. void removeKeyStrokeBinding(KeyStroke)
void removeBindings()
(inKeymap
)Remove one or all key bindings from a keymap. void setDefaultAction(Action)
Action getDefaultAction()
(inKeymap
)Set or get the default action. This action is fired if a keystroke is not explicitly bound to an action. Action[] getBoundActions()
KeyStroke[] getBoundKeyStrokes()
(inKeymap
)Get an array containing all of the bound actions or keystrokes in a keymap.
Reading and Writing Text Method Description void JTextComponent.read(Reader, Object)
void JTextComponent.write(Writer)
(inJTextComponent
)Read or write text. void read(Reader, Document, int)
void read(InputStream, Document, int)
(inEditorKit
)Read text from a stream into a document. void write(Writer, Document, int, int)
void write(OutputStream, Document, int, int)
(inEditorKit
)Write text from a document to a stream.
API for Displaying Text from a URL Method or Constructor Description JEditorPane(URL)
JEditorPane(String)
(inJEditorPane
)Create an editor pane loaded with the text at the specified URL. setPage(URL)
setPage(String)
(inJEditorPane
)Load an editor pane (or text pane) with the text at the specified URL. URL getPage()
(inJEditorPane
)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 ComponentsUses 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 ClassUses 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
Using Swing Components |