Text Entry Components:

On Social Media

Swing provides several such components from the most basic single-line text field to the
sophisticated multi-line components that understand and support HTML and RTF. Swing
also provides a convenient class of entry fields specifically suited for entering passwords.

ConstructorDescription
JTextComponent()Creates a new JTextComponent.
MethodsDescription
String getText()Returns the text contained in this TextComponent.
String getText(int offs, int len)Fetches a portion of the text represented by the
component.
void setText(String t)Sets the text of this TextComponent to the specified text.
void setEditable(boolean b)Sets the specified boolean to indicate whether or not this
TextComponent should be editable.
void selectAll()Selects all the text in the TextComponent.
void select(int selectionStart, int selectionEnd)Selects the text between the specified start and end
positions.

JTextFeild is a lightweight componenet that allows the editing of a single line of text.

ConstructorDescription
JTextField()Constructs a new TextField.
JTextField(Document doc, String text, int columns)Constructs a new JTextField that uses the given text storage model and the given number of columns.
JTextField(int columns)Constructs a new empty TextField with the specified number of columns.
JTextField(String text) Constructs a new TextField initialized with the specified text.
JTextField(String text, int columns)Constructs a new TextField initialized with the specified text and columns.
MethodsDescription
void addActionListener(ActionListener l)Adds the specified action listener to receive action events from this textfield.
void setFont(Font f)Sets the current font.
void setColumns(int columns)Sets the number of columns in this TextField.

Example of JTextField

A JTextArea is a multi-line area that displays plain text. It is intended to be a lightweight component.

ConstructorsDescription
JTextArea()Constructs a new TextArea.
JTextArea(int rows, int cols)Constructs a new empty TextArea with the specified number of rows and columns.
JTextArea(String text)Constructs a new TextArea with the specified text displayed.
JTextArea(String text, int rows, int cols)Constructs a new TextArea with the specified text and number of rows and columns.
JTextField(String text, int cols)Constructs a new TextField initialized with the specified text and columns.
Methods Description
void append(String str)Appends the given text to the end of the document.
int getColumns()Returns the number of columns in the TextArea.
void setColumns(int columns)Sets the number of columns for this TextArea.
void insert(String str, int pos)Inserts the specified text at the specified position.
void setFont(Font f)Sets the current font.
int getRows()Returns the number of rows in the TextArea.
void setRows(int rows)Sets the number of rows for this TextArea.

A JEditorPane is a region that is designed to parse and edit specific types of structured  text content.  If a scrollbar region is required, this component should be placed inside a JScrollPane object.

Constructors Description
JEditorPane()Creates a new JEditorPane.
JEditorPane(String url)Creates a JEditorPane based on a string containing a URL specification.
JEditorPane(String type, String text)Creates a JEditorPane that has been initialized to the given text.
JEditorPane(URL initialPage)Creates a JEditorPane based on a specified URL for input.

JPasswordField is a lightweight component that allows the editing of a single line of text where the view indicates something was typed, but does not show the original characters.

ConstructorsDescription
JPasswordField()Constructs a new JPasswordField, with a default document, null starting text string, and 0 column width.
JPasswordField(int columns)Constructs a new empty JPasswordField with the specified number of columns.
JPasswordField(String text)Constructs a new JPasswordField initialized with the specified text.displayed.
JPasswordField(String text, int columns)Constructs a new JPasswordField initialized with the specified text and columns.
MethodsDescription
char getEchoChar()Returns the character to be used for echoing.
void setEchoChar(char c) Sets the echo character for this JPasswordField.
char[]getPassword()Returns the text contained in this TextComponent.

Example for JPasswordFeild

A text component that can be marked up with attributes that are represented graphically.

ConstructorsDescription
JTextPane()Creates a new JTextPane.
JTextPane(StyledDocument doc)Creates a new JTextPane, with a specified document model.


On Social Media

Leave a Reply

Your email address will not be published. Required fields are marked *