Java Reference
In-Depth Information
EXAMPLES
String inputString = ioComponent.getText();
ioComponent.setText("Hello out there!");
ioComponent may be an instance of either of the classes JTextField or JTextArea .
text area
A text area is an object of the class JTextArea . A text area is the same as a text field
except that it allows multiple lines. Two parameters to the constructor for JTextArea
specify the minimum number of lines and the minimum number of characters per
line that are guaranteed to be visible. You can enter any amount of text in a text area,
but only a limited number of lines and a limited number of characters per line will be
visible. For example, the following creates a JTextArea named theText that will have
at least 5 lines and at least 20 characters per line visible:
JTextArea
JTextArea theText = new JTextArea(5, 20);
There is also a constructor with one additional String parameter for the string
initially displayed in the text area. For example,
JTextArea theText = new JTextArea("Enter\ntext here.", 5, 20);
Note that a string value can be multiple lines because it can contain the new-line
character '\n' .
A JTextField has a similar constructor with a String parameter, as in the
following example:
JTextField ioField =
new JTextField("Enter numbers here.", 30);
If you look at Display 17.12, you will see that both JTextField and JTextArea
are derived classes of the abstract class JTextComponent . Most of the methods for
JTextField and JTextArea are inherited from JTextComponent and so JTextField
and JTextArea have mostly the same methods with the same meanings except for
minor redefinitions to account for having just one line or multiple lines. Display 17.18
describes some methods in the class JTextComponent . All of these methods are
inherited and have the described meaning in both JTextField and JTextArea .
You can set the line-wrapping policy for a JTextArea using the method
setLineWrap . The method takes one argument of type boolean . If the argument is
true , then at the end of a line, any additional characters for that line will appear on
the following line of the text area. If the argument is false , the extra characters will
be on the same line and will not be visible. For example, the following sets the line
wrap policy for the JTextArea object named theText so that at the end of a line, any
additional characters for that line will appear on the following line:
setLineWrap
theText.setLineWrap( true );
You can specify that a JTextField or JTextArea cannot be written in by the user.
To do so, use the method setEditable , which is a method in both the JTextField
output-only
setEditable
 
Search WWH ::




Custom Search