  // The Swing Exercise Solution

  import javax.swing.*;
  import java.awt.*;
  import java.awt.event.*;

  public class SwingExercise extends JFrame implements ActionListener
  {
	
	private JButton button;
	private JTextArea textArea;
	private JTextField status;
	private JCheckBox editable;
	private JComboBox colorBox;
	private boolean isTextEditable = true;
	
	public SwingExercise()
	{
	  super("Swing Exercise");

	  // controls panel

	  JPanel controls = new JPanel(new FlowLayout());
	  this.button = new JButton("Clear Text");
	  this.button.addActionListener(this);	
	  controls.add(this.button);

	  editable = new JCheckBox("Read-Only");
	  editable.addActionListener(this);
	  controls.add(this.editable);

	  controls.add(new JLabel("  Text Color:"));
	  String[] items = {"Black", "Blue", "Green", "Red"};
	  colorBox = new JComboBox(items);
	  colorBox.addActionListener(this);
	  controls.add(colorBox);

	  // status TextField
	  this.status = new JTextField();
	  this.updateStatus("Application Started.");

	  // main area, with scrolling and wrapping

	  this.textArea = new JTextArea(20,40);
	  JScrollPane p = new JScrollPane(this.textArea,
			JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
			JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
	  this.textArea.setWrapStyleWord(true);

	  // default layout is border layout for frame/jframe

	  this.getContentPane().add("North",controls);
	  this.getContentPane().add("South",this.status);
	  this.getContentPane().add("Center",p);

	  this.pack();  //set the size of the frame according 
			//to the component's preferred sizes
	  this.show();	//make the frame visible
	}
	
	public void actionPerformed(ActionEvent e)
	{
	  if (e.getSource().equals(button))
	  {
	    this.textArea.setText("");
	    this.updateStatus("Text Area Cleared.");
	  }
	  else if (e.getSource().equals(editable))
	  {
	    this.isTextEditable = !this.isTextEditable;
	    this.textArea.setEditable(this.isTextEditable);
	    this.updateStatus("Set Text Area Editable - "+this.isTextEditable);
 	  }
	  else if (e.getSource().equals(colorBox))
	  {
	    String selection = (String) colorBox.getSelectedItem();
	    if (selection.compareTo("Red")==0)
		this.textArea.setForeground(Color.red);
	    else if (selection.compareTo("Green")==0)
		this.textArea.setForeground(Color.green);
	    else if (selection.compareTo("Blue")==0)
		this.textArea.setForeground(Color.blue);
	    else this.textArea.setForeground(Color.black);
	    this.updateStatus("Set Text Color to - " + selection);
	  }
	}

	private void updateStatus(String theStatus)
	{
	  this.status.setText("Status: " + theStatus);
	}

	public static void main(String args[])
	{
	   new SwingExercise();
	}
  }
