Create Software Components 

Using Java Level 2

 

 

Course Info

Scheme

Resources

Tutorials

Java Demos

Utilities

Links


 

  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:-

  • String getSelectedText()

Returns the selected text contained in this TextComponent.

  • String getText()

Returns the text contained in this TextComponent.

  • setText(String s)

Sets the text in the TextComponent.

  • boolean isEditable()

Returns the boolean indicating whether this TextComponent is editable or not.

  • setEditable(boolean  b)

Set the specified boolean to indicate whether or not this TextComponent should be editable.

  • select(int selectionStart, int selectionEnd)

Selects the text between the specified start and end positions

  • selectAll()

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~

  • JTextField()

Creates a text field without any initial text in the field.

  • JTextField(String s)

Creates a text field with the initial text in the field specified by the string s.

  • JTextField(String s, int i)

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 

  • JTextField(int i)

Creates a text field with the desired column width specified by the integer argument.  

 

Here are a couple of useful JTextField methods:-  

~Methods~

  • setFont(Font f)

Sets the current font.

  • setHorizontalAlignment(int alignment)

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 ...

  1. at least one panel to contain some text fields
  2. four text fields, each created using a different constructor, (as shown in the picture above.)
  3. Set one of the text fields to be uneditable.
  4. 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.
  5. 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.
  6. 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~

  • JTextArea()

Creates a text area without any initial text or a specific size.

  • JTextArea(int rows, int columns)

Creates a text area with the specified number of rows and columns.

  • JTextArea(String s)

Creates a text area with the initial text in the area specified by the String s. 

  • JTextArea(String text, int rows, int columns)

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~

  • int getLineCount()

Returns the number of lines contained in the area.

  • insert(String s, int pos)

Inserts the specified text at the specified position.

  • setFont(Font f)

Sets the current font.

  • setLineWrap(boolean b)

Sets the line-wrapping policy of the text area.  If b is true then lines will wrap, if b is false they won't.

  • setWrapStyleWord(boolean b)

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 ...

  1. at least one panel to contain a text area
  2. 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.

  1. set the text area to be uneditable using the setEditable method.
  2. 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.
  3. 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 ...

  1. at least one panel to contain a text area
  2. 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.

  1. Create a JScrollPane and add the JTextArea to it.
  2. set the font of the JTextArea to to Serif, italic and 12 points in size.  The Font class is explained in the Graphics tutorial.
  3. set the panel background color to any color of your choice

 

 

  Site Home 

Java Home   

  Forum  

Course Info

Welcome

Overview

Assessment

Qualification

Scheme of Work

Assignments

Resources

Information

Blackboard

Learning Center

Web Materials

Reading

Java Demos

Utilities

Links

Lecture Materials

Tutorials & Notes

Exercises

Activities

Quizzes

 

Site Home

Top

Unit Home

ADR 2002