Java Swing Tutorial - Java Swing Text Components








We can categorize the text components based on two criteria:

  • number of lines in text
  • type of text they can handle.

Based on the number of lines of text that a text component can handle, we can further categorize them as follows:

  • Single-line text component
  • Multiline text component

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 component
  • Styled text component

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

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.

IDMethod/Description
1Keymap addKeymap(String name, Keymap parentKeymap)
Adds a new keymap to the keymap hierarchy of the component.
2void copy()
Copies the selected text to the system clipboard.
3void cut()
Moves the selected text to the system clipboard.
4Action[] getActions()
the command list for the text editor.
5Document getDocument()
Returns the model for the text component.
6Keymap getKeymap()
Returns the currently active keymap for the text component.
7static Keymap getKeymap(String keymapName)
Returns the keymap associated with this document with the name keymapName.
8String getSelectedText()
Returns the selected text in the component. It returns null if there is no selected text or the document is empty.
9int getSelectionEnd()
Returns the end position of the selected text.
10int getselectionStart()
Returns the start position of the selected text.
11String getText()
Returns the text that is contained in this text component.
12String 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.
13TextUI getUI()
Returns the user-interface factory for the text component.
14boolean isEditable()
Returns true if the text component is editable. Otherwise, returns false.
15void paste()
Transfers the content of the system clipboard to the text component model.
16void print()
It displays a print dialog and lets we print the content of the text component without a header and footer.
17void read(Reader source, Object description) throws IOException
Reads the content from the source stream into the text component
18void 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.
19void select(int start, int end)
Selects the text between the start and end positions.
20void selectAll()
Selects all text in a text component
21void setDocument(Document doc)
Sets the document (that is, the model) for the text component.
22void setEditable(boolean editable)
Sets a text component as editable if editable is true. If editable is false, sets the text component as non-editable.
23void setKeymap(Keymap keymap)
Sets the keymap for the text component.
24void setSelectionEnd(int end)
Sets the end position of selection.
25void setSelectionStart(int start)
Sets the start position of selection.
26void setText(String newText)
Sets the text of the text component.
27void setUI(TextUI newUI)
Sets new UI for the text component.
28void updateUI()
Reloads the pluggable UI for the text component.
29void write(Writer output)
Writes the contents of the text component to a stream defined by output.