|
| |
A JTextArea is a text component that allows more than a
single line.
Here's an example of how to use text areas:
package swingExamples;
import java.awt.Color;
import java.awt.Container;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextArea;
class SimpleTextAreasPanel extends JPanel
{
public SimpleTextAreasPanel()
{
setBackground(Color.cyan);
JTextArea area1 = new JTextArea(4, 10);
// 4 lines of 10 characters
area1.setText("Hello!\nI am an editable\ntext area!");
add(area1);
JTextArea area2 =
new JTextArea("A non-editable text area...");
area2.setEditable(false);
area2.setColumns(20);
area2.setRows(3);
area2.setForeground(Color.BLUE);
add(area2);
JTextArea area3 = new JTextArea(
"I'm another non-editable text area,\nyellow-on black");
area3.setEditable(false);
area3.setBackground(Color.BLACK);
area3.setForeground(Color.YELLOW);
add(area3);
}
}
class SimpleTextAreasFrame extends JFrame
{
public SimpleTextAreasFrame()
{
setTitle("SimpleTextAreas");
setSize(300, 200);
setDefaultCloseOperation(EXIT_ON_CLOSE);
Container contentPane = getContentPane();
contentPane.add( new SimpleTextAreasPanel() );
}
}
public class SimpleTextAreas
{
public static void main(String[] args)
{
SimpleTextAreasFrame frame = new SimpleTextAreasFrame();
frame.setVisible(true);
}
}
|
which produces:

Notice that the non-editable text area does not have a different background
color than the editable text area. Also, while the user cannot input anything
into the non-editable text area, s/he can select and copy text from it into the
paste buffer.
Note also that a JTextArea has no standard built-in border decorations, which makes it
rather boring. We'll see how to improve matters soon.
|