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](../Images/dividerdash.gif)
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. |
|
|
![dividerdash](../Images/dividerdash.gif)
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.
|
|
Console display:
![fleas](fleas.png) |
![tecode1](tecode1.png)
![tecode2](tecode2.png)
![dividerdash](../Images/dividerdash.gif)
|
|
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.
JTextArea( ) - creates a text display area without any text displayed.
JTextArea(String text) - creates a text display area with the specified text displayed.
JTextArea(int row, int column) - creates a text display area with specified number of
rows and columns with no text displayed.
JTextArea(String text, int row, int column) - creates a text display area with specified number of rows and columns with specified text displayed.
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](wow1.gif) |
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](wow11.png) |
Console Display:
![wow12](wow12.png)
|
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](../Images/dividerdash.gif)
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.)
![splitpic1](splitpic1.png)
![dogwrap](dogwrap.png)
![dogwrap1](dogwrap1.png)
![split2](split2.png)