Java
Swing - GUIs LESSON NOTE - JTEXTAREA JTEXTAREA CONSTRUCTOR We
create a JTextArea by specifying the number of rows
and columns. In the following
statement, the JTextArea will have 5 rows and 20
columns: JTextArea ta =
new JTextArea(5,20); EDITABILITY We
can set whether a JTextArea can be edited or not by
using the setEditable(Boolean) method.
Here is an example to set it to be uneditable: ta.setEditable(false); CHANGING TEXT We
can set the text inside a JTextArea by using the setText(String)
method. This will replace any existing
text. ta.setText("hello"); We
can add text to the end of the current text in a JTextArea
by using the append(String) method. ta.append("
there"); We
can get the text in a JTextArea by using the getText()
method. String s = ta.getText(); We
can add new text to the beginning of a JTextArea by
using the following: ta.setText("Put
me at the start\n" + ta.getText()); FULL CODE EXAMPLE Here
is an example that displays some of the functionality of a JTextArea shown above:
JTEXTAREA WITH SCROLLING To
make a JTextArea have scrollbars, we use the JScrollPane object.
The code looks like this: Container
cp = this.getContentPane(); cp.setLayout(new FlowLayout()); JTextArea ta = new JTextArea(5,20); JScrollPane sp = new JScrollPane(ta); cp.add(sp); The
result of the above code is If
we want to use code to add to a JTextArea inside a JScrollPane, then we still use the setText(String) method on
the JTextArea.
ta.setText("hello"); |
|
|