We can categorize the text components based on two criteria:
Based on the number of lines of text that a text component can handle, we can further categorize them as follows:
A single line text component can handle one line of text. JTextField, JPasswordField, and JFormattedTextField are single-line text components.
A multiline text component can handle multiple lines of text. JTextArea, JEditorPane, and JTextPane classes are multiline text components.
Based on the type of the text that a text component can handle, we can categorize text components as follows:
Plain text has no format while the styled text can have format, such as bold, italic, underlined, etc., font, and color.
A plain text means that the entire text contained in the text component is displayed using only one style.
JTextField, JPasswordField, JFormattedTextField, and JTextArea are plain text components. JEditorPane and JTextPane are styled components.
JTextComponent is an abstract class and it is the ancestor of all Swing text components. It includes common functionalities that are available to all text components.
The following table lists some commonly used methods of text components that are included in the JTextComponent class.
|1||Keymap addKeymap(String name, Keymap parentKeymap)|
Adds a new keymap to the keymap hierarchy of the component.
Copies the selected text to the system clipboard.
Moves the selected text to the system clipboard.
the command list for the text editor.
Returns the model for the text component.
Returns the currently active keymap for the text component.
|7||static Keymap getKeymap(String keymapName)|
Returns the keymap associated with this document with the name keymapName.
Returns the selected text in the component. It returns null if there is no selected text or the document is empty.
Returns the end position of the selected text.
Returns the start position of the selected text.
Returns the text that is contained in this text component.
|12||String getText(int offset, int length) throws BadLocationException|
Returns a portion of the text contained in the text component starting at the offset position and number of characters.
Returns the user-interface factory for the text component.
Returns true if the text component is editable. Otherwise, returns false.
Transfers the content of the system clipboard to the text component model.
It displays a print dialog and lets we print the content of the text component without a header and footer.
|17||void read(Reader source, Object description) throws IOException |
Reads the content from the source stream into the text component
|18||void replaceSelection(String newContent)|
Replaces the selected content with the newContent. If there is no selected content, it inserts the newContent. If newContent is null or an empty string, it removes the selected content.
|19||void select(int start, int end)|
Selects the text between the start and end positions.
Selects all text in a text component
|21||void setDocument(Document doc)|
Sets the document (that is, the model) for the text component.
|22||void setEditable(boolean editable)|
Sets a text component as editable if editable is true. If editable is false, sets the text component as non-editable.
|23||void setKeymap(Keymap keymap)|
Sets the keymap for the text component.
|24||void setSelectionEnd(int end)|
Sets the end position of selection.
|25||void setSelectionStart(int start)|
Sets the start position of selection.
|26||void setText(String newText)|
Sets the text of the text component.
|27||void setUI(TextUI newUI)|
Sets new UI for the text component.
Reloads the pluggable UI for the text component.
|29||void write(Writer output)|
Writes the contents of the text component to a stream defined by output.