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.
ID | Method/Description |
---|---|
1 | Keymap addKeymap(String name, Keymap parentKeymap) Adds a new keymap to the keymap hierarchy of the component. |
2 | void copy() Copies the selected text to the system clipboard. |
3 | void cut() Moves the selected text to the system clipboard. |
4 | Action[] getActions() the command list for the text editor. |
5 | Document getDocument() Returns the model for the text component. |
6 | Keymap getKeymap() 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. |
8 | String getSelectedText() Returns the selected text in the component. It returns null if there is no selected text or the document is empty. |
9 | int getSelectionEnd() Returns the end position of the selected text. |
10 | int getselectionStart() Returns the start position of the selected text. |
11 | String getText() 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. |
13 | TextUI getUI() Returns the user-interface factory for the text component. |
14 | boolean isEditable() Returns true if the text component is editable. Otherwise, returns false. |
15 | void paste() Transfers the content of the system clipboard to the text component model. |
16 | void print() 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. |
20 | void selectAll() 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. |
28 | void updateUI() 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. |