Copyright ©1996-1998 by Axel T. Schreiner.  All Rights Reserved.



TextArea, text with global attributes

[ Applet]

Example N experiments with a TextArea. Font attributes and colors can be changed for the text as a whole.

A TextArea displays a multi-line text in a single font and color.



Implementation

The attributes are set by buttons using a common ActionListener. Because font attributes can only be changed by introducing a new Font object , they are recorded locally:
{apps/gui/N.java}
import awt.Button;
import awt.Panel;
import awt.TextArea;
import java.awt.*;
import java.awt.event.*;

/** A class to test text attributes in a text area */
public class N extends Panel {
protected TextArea t;
protected String font; protected int style; protected int size;    public N () {
super(new BorderLayout());

add("Center", t = new TextArea(10, 40));
t.setFont(new Font(font = "SansSerif", style = Font.PLAIN, size = 12));

final ActionListener opt = new ActionListener() {
public void actionPerformed (ActionEvent e) {
t.requestFocus();
String cmd = e.getActionCommand();
if (cmd.equals("quit"))
System.exit(0);
else if (cmd.equals("serif"))
t.setFont(new Font(font = "Serif", style, size));
else if (cmd.equals("sans"))
t.setFont(new Font(font = "SansSerif", style, size));
else if (cmd.equals("mono"))
t.setFont(new Font(font = "Monospaced", style, size));
else if (cmd.equals("plain"))
t.setFont(new Font(font, style = Font.PLAIN, size));
else if (cmd.equals("bold"))
t.setFont(new Font(font, style = Font.BOLD, size));
else if (cmd.equals("italic"))
t.setFont(new Font(font, style = Font.ITALIC, size));
else if (cmd.equals("boldItalic"))
t.setFont(new Font(font, style = Font.BOLD+Font.ITALIC, size));
else if (cmd.equals("larger"))
t.setFont(new Font(font, style, size+=2));
else if (cmd.equals("smaller") && size > 2)
t.setFont(new Font(font, style, size-=2));
else if (cmd.equals("black"))
t.setForeground(Color.black);
else if (cmd.equals("red"))
t.setForeground(Color.red);
else if (cmd.equals("blue"))
t.setForeground(Color.blue);
else if (cmd.equals("green"))
t.setForeground(Color.green);
}
};



Button b;
Panel p;
add("North", p = new Panel(new GridLayout(2, 7)));
p.add(b = new Button("serif")); b.addActionListener(opt);
b.setFont(new Font("Serif", Font.PLAIN, 12));
p.add(b = new Button("sans")); b.addActionListener(opt);
b.setFont(new Font("SansSerif", Font.PLAIN, 12));
p.add(b = new Button("mono")); b.addActionListener(opt);
b.setFont(new Font("Monospaced", Font.PLAIN, 12));
p.add(b = new Button("plain")); b.addActionListener(opt);
b.setFont(new Font("SansSerif", Font.PLAIN, 12));
p.add(b = new Button("bold")); b.addActionListener(opt);
b.setFont(new Font("SansSerif", Font.BOLD, 12));
p.add(b = new Button("italic")); b.addActionListener(opt);
b.setFont(new Font("SansSerif", Font.ITALIC, 12));
p.add(b = new Button("boldItalic")); b.addActionListener(opt);
b.setFont(new Font("SansSerif", Font.BOLD+Font.ITALIC, 12));
p.add(b = new Button("larger")); b.addActionListener(opt);
p.add(b = new Button("smaller")); b.addActionListener(opt);
p.add(b = new Button("black")); b.addActionListener(opt);
p.add(b = new Button("red")); b.addActionListener(opt);
b.setForeground(Color.red);
p.add(b = new Button("blue")); b.addActionListener(opt);
b.setForeground(Color.blue);
p.add(b = new Button("green")); b.addActionListener(opt);
b.setForeground(Color.green);
p.add(b = new Button("quit")); b.addActionListener(opt);
}
}
{}

21/Apr/1998