datalogo
Return to Unit Menu |  JavaBitsNotebook.com | MathBits.com | Terms of Use  | JavaMathBits.com

Creating a JTextField or JTextArea

In Swing, the JTextField and JTextArea are components that allow the user to enter (or edit) a text-based response. The JTextField class specifically allows the editing of a single line text. If the use of multiple lines is desired, the JTextArea class is used

When using JTextFiled or JTextArea, it is customary to see some type of "prompt" telling the user what task needs to be done, along with a button such as SUBMIT or ENTER for use when the user has completed the task.

dividerdash

JTextField

Only one line of user response will be accepted. If multiple lines are desired, JTextArea will be needed.

As with all action events, when an event listener registers an event, the event is processed and the data in the text field can be utilized in the program.

reddot JTextField() - creates new text field
reddot JTextField(int columns) - creates new text field with specified number of columns
reddot JTextField(String text) - creates new text field with specified text
reddot JTextField(String text, int columns) - creates new text field with specified text and columns.

reddot getText() will be used to retreive the text that was entered in the text field.

dividerdash

Create and Activate a JTextField:
 

Example: The user has been asked to enter a message. If SUBMIT is clicked after the message is entered, the message will appear in the console window. 

textpic1
tf2

Console display:
fleas

   tecode1
  tecode2

dividerdash

JTextArea
 

If multiple lines entered by the user are desired, JTextArea will be needed. JTextArea is described as a multi-line area for displaying plain text.

reddot JTextArea( ) - creates a text display area without any text displayed.
reddot JTextArea(String text) - creates a text display area with the specified text displayed.
reddot JTextArea(int row, int column) - creates a text display area with specified number of
rows and columns with no text displayed.
reddot JTextArea(String text, int row, int column) - creates a text display area with specified number of rows and columns with specified text displayed.

reddot getText() will be used to retrieve the text that was entered in the text area.

NEW INFORMATION:

Java String split( ) method

When working with strings we often search for a specific character within the string, such as a character letter or even a whitespace (" ") using indexOf. We even used this strategy to isolate words within a sentence.

We are now going to look at a powerful built-in method for separating words from a sentence, assuming the words are separated by whitespace. This method will also store the words in an array. And this can all be accomplished with ONE call line of code.

wow String sentence = "My dog has fleas.";
//split the string at each whitespace encountered
//AND store these words in an array

String [ ] words = sentence.split(" ");
wow11

Console Display:

wow12


We will be putting this new method to work in our JTextArea example.
(You may also encounter this method used with "\\s" instead of " ". The \s is an expression character for white space and the extra \ is needed for the expression to compile.)

JTextArea area;
area = new JTextArea("Hello, world!");
area.setBounds(40,75,250,200);
area.setLineWrap(true);
area.setWrapStyleWord(true);
panel.add(area);

The first line of text will disappear behind the edge of the viewing area unless you tell the text to wrap. Telling the text to wrap by word will break the text by words as it wraps instead of wrapping by characters (ignoring words). Both commands are needed.

dividerdash

Create and Activate a JTextArea:
 

Example: The user is going to see a screen containing a text message. The user can edit or add to the message. A button at the bottom of the screen will display the number of words in the text display at any given time. 
(If you place two spaces between words, it will be counted as an extra word.)

splitpic1dogwrap

dogwrap1
split2

divider
Return to Unit Menu |  JavaBitsNotebook.com | MathBits.com | Terms of Use  | JavaMathBits.com

Notice: These materials are free for your on-line use at this site, but are not free for the taking.
Please do not copy these materials or re-post them to the Internet, as it is copyright infringement.
If you wish hard-copies of these materials, refer to our subscription area, JavaMathBits.com.
Help us keep these resources free. Thank you.