|
Swing
Text Components
This
tutorial describes the Swing JTextField and JTextArea
classes.
JTextComponent
Class
JTextField
Class
JTextArea
Class
Unscrollable
JTextField
Scrollable
JTextField
JTextComponent Class
Swing
text components can be used to display information or get text based
information from a user. Two useful text components are JTextField
and JTextArea. Both of these components descend from the JTextComponent
class from which they inherits
many methods.

The
applet below shows a JTextField (at the top) and a JTextArea
(below). Normally both types of components are editable
by default. In this example, the JTextField is editable
and when the user presses the return key in the text area, text will be
copied to the JTextArea.
The JTextArea
was set to be uneditable. If the user keeps copying text to the JTextArea
and the display area becomes too small a scrollbar appears. JTextAreas
do not naturally implement scrollbars. You have to add a JTextArea
to a JScrollPane for it to contain scrollbars.
On
clicking Answer on the Options menu,
the last line of the verse is displayed and highlighted.
~~~Applet~~~
Some useful
methods inherited from JTextComponent that can be used by JTextField
and JTextArea are:-
|
|
Returns
the selected text contained in this TextComponent.
|
|
|
Returns
the text contained in this TextComponent.
|
|
|
Sets
the text in the TextComponent.
|
|
|
Returns
the boolean indicating whether this TextComponent is editable or not.
|
|
|
Set
the specified boolean to indicate whether or not this TextComponent should be
editable.
|
|
|
Selects
the text between the specified start and end positions
|
|
|
Selects
all the text in the TextComponent.
|
JTextField
A JTextField
is an area that displays a single line of plain text. You would use a
text field to get a small amount of textual information from the user. You can create
a JTextField using one of the four constructors shown below:-
~Constructors~
|
|
Creates
a text field without any initial text in the field.
|
|
|
Creates
a text field with the initial text in the field specified by the string s.
|
|
|
Creates
a text field with the initial text in the field specified by the string s.
The integer argument specifies the desired column width of the text field
|
|
|
Creates
a text field with the desired column width specified by the integer
argument.
|
Here
are a couple of useful JTextField methods:-
~Methods~
|
|
Sets
the current font.
|
|
|
Sets
the horizontal alignment of the text. Alignment can be set to JTextField.LEFT,
JTextField.CENTER
or JTextField.RIGHT.
|
The picture
below shows these four different text fields each created using a different constructors.
The constructor used is shown in the label next to the text field.

The last two
constructors expect an integer argument. This integer, 10 in
the example, indicates the number of columns in the field. The fields
preferred width is calculated from this and also depends upon the field's
current font. Specifying the number of columns does not limit the number
of characters the user can enter.
~Now
try the activity~
|
Activity A |
Using appropriate
methods from the JTextComponent (listed above) and JComponent,
create an applet with ...
- at least one panel to contain some text fields
- four text fields, each created using a different constructor, (as
shown in the picture above.)
- Set one of the text fields to be uneditable.
- set the text color of one of the text fields to dark blue, (hint,
try using an RGB colour). The
Color class is
described in the Graphics
tutorial.
- set the font of one of the text fields to to
Serif, italic and
12 points in size. The
Font class is explained
in the Graphics tutorial.
- set the panel background color to any color of your choice
|
JTextArea
A
JTextArea is a multi-line area that displays plain text.
Although a
JTextArea can display text in any font, all of the text is in the same
font. You would use a
JTextArea to allow the user to enter unformatted
text of any length or to display unformatted information.
The applet
below shows two different JTextAreas. They are both editable
but the left JTextArea does not implement scrollbars. In
this example, entering text into the area causes the area to increase in size
if the display area becomes too small. Thus, it would have been better
to set this area to uneditable. The right JTextArea
implements a vertical scrollbar. As you will see, if scrolling behavior is
desired you can place a JTextArea inside a JScrollPane.
~~~Applet~~~
You can create
a JTextArea using one of the four constructors shown below:-
~Constructors~
|
|
Creates
a text area without any initial text or a specific size.
|
|
|
Creates
a text area with the specified number of rows and columns.
|
|
|
Creates
a text area with the initial text in the area specified by the String s.
|
|
|
Creates
a text area with the initial text in the area specified by the String s and the specified
number of rows and columns.
|
Here
are some useful JTextArea methods:-
~Methods~
|
|
Returns
the number of lines contained in the area.
|
|
|
Inserts
the specified text at the specified position.
|
|
|
Sets
the current font.
|
|
|
Sets
the line-wrapping policy of the text area. If b is true then lines
will wrap, if b is false they won't.
|
|
|
Sets
the style of wrapping used if the text area is wrapping lines. If b is true
then wrapping will not occur in the middle of words, if b is false it
will.
|
Other
useful JTextArea methods are inherited from JTextComponent
and JComponent.
Unscrollable
JTextArea
To
create a JTextArea without scrolling capabilities just use one
of the constructors listed in the table above. The left JTextArea
shown in the applet was created using the the third constructor:-
textArea1 = new JTextArea(
"This is an editable JTextArea." +
"\nAlthough it can display text in any font, all of the " +
"text is in the same font." +
"\n\nIt does not implement scrollbars." +
"\nWhat happens when you enter more text?\n");
The
initial string specified is quite long. The +
operator joins the separate strings together into one string. The \n
combination specifies a newline character.
The picture below shows the JTextArea. You can see
that lines are not wrapped and so the width of the display area is the width
of the longest line.

The
easiest way to correct this is to use the setLineWrap and setWrapStyleWord
methods.
//set
the area so that lines are wrapped
textArea1.setLineWrap(true);
//set
the area so that wrapping occurs at word boundaries
//rather
than in the middle of words
textArea1.setWrapStyleWord(true);
|
The
picture on the right shows that the dimensions of the display area
may still not be as desired. This could be corrected by using
the fourth constructor listed in the table above instead of the third
and specifying the number of rows and columns. Or just use the setColumns
method:-
//set
the column width of the text area
textArea1.setColumns(20);

That
looks about right. |
 |
~Now
try the activity~
|
Activity B |
Using appropriate
methods from the JTextComponent (listed above), JComponent
and JTextArea classes, create an applet with ...
- at least one panel to contain a text area
- one
JTextArea that displays the following text ...
The sun was shining on the
sea,
Shining with all his might:
He did his very best to make
The billows smooth and bright--
And this was odd, because it was
The middle
of the night.
- set the text area to be uneditable using the
setEditable
method.
- set the font of one of the
JTextArea to Serif, italic and
12 points in size. The
Font class is explained
in the Graphics tutorial.
- set the panel background color to any color of your choice
|
Scrollable
JTextArea
To
create a JTextArea with scrolling capabilities you have to create
a JTextArea and add it to a JScrollPane.
The right JTextArea shown in the applet was created using the the
third constructor listed in the table above:-
textArea2 = new JTextArea(
"This JTextArea has been added to a JScrollPane. " +
"\nThis means that vertical and horizontal scrollbars " +
"will appear if necessary. " +
"\n\nEnter more text to see a vertical scrollbar.\n");
The
initial string specified is quite long. The +
operator joins the separate strings together into one string. The \n
combination specifies a newline character.
The
setLineWrap and setWrapStyleWord methods are used to
set the wrapping policy.
//set
the area so that lines are wrapped
textArea2.setLineWrap(true);
//set
the area so that wrapping occurs at word boundaries
//rather
than in the middle of words
textArea2.setWrapStyleWord(true);
To
make a text area scrollable you create a JScrollPane and specify
the text area to be added to the JScrollPane in the argument:-
JScrollPane
scr = new JScrollPane(textArea2);
This
creates a scrollable text area. The scrollbars only appear when
necessary - i.e. if the display area becomes too small to display additional
text.
You
can specify the scroll bar policy - this means setting if a scrollbar
appears always, never or only when necessary:-
//set
the vertical scrollbar to show always
scr.setVerticalScrollBarPolicy
(scr.VERTICAL_SCROLLBAR_ALWAYS);
//set
the horizontal scrollbar to show never
scr.setHorizontalScrollBarPolicy
(scr.HORIZONTAL_SCROLLBAR_NEVER);
The
default scroll bar policy for a scroll pane is HORIZONTAL_SCROLLBAR_AS_NEEDED
and VERTICAL_SCROLLBAR_AS_NEEDED
You
can specify a preferred size for the scrolling pane (whether this preferred
size is implemented or not depends on the layout manager):-
//set
the scroll panes dimensions
scr.setPreferredSize(new Dimension(250, 120));
You
don't add a JTextArea to a container you actually add the JScrollPane
(this makes sense since the JTextArea has already been
added to the JScrollPane):-
//add
the scroll pane to a panel
myPanel.add(scr);
You
should end up with something like this:-

~Now
try the activity~
|
Activity C |
Using appropriate
methods from the JTextComponent (listed above), JComponent
and JTextArea classes,
create an applet with ...
- at least one panel to contain a text area
- one
JTextArea that displays the following text ...
The sun was shining on the
sea,
Shining with all his might:
He did his very best to make
The billows smooth and bright--
And this was odd, because it was
The middle
of the night.
- Create a
JScrollPane and add the JTextArea
to it.
- set the font of the
JTextArea to to Serif, italic and
12 points in size. The
Font class is explained
in the Graphics tutorial.
- set the panel background color to any color of your choice
|
|