Arrays in JTextAreas.

If you choose to type this (and that is a bad choice, writing it out is easier), it can not be longer than one page. You must hand this in on paper.

1. Put this code in java. Run it. On a seperate sheet of paper, draw what appears.

import javax.swing.*;
   import java.awt.*;
   import java.awt.event.*;
   import java.applet.Applet;
public class ArrayTextArea extends Applet implements ActionListener
   {
   String name [] = {"Boole", "Turing", "Gosling",    "Sutherland"};
   public void init ()
   {
   JTextArea box = new JTextArea (10, 10);
   for (int i = 0 ; i < name.length ; i++)
   box.append ((i + 1) + " " + name [i] + "\n");
   add (box);
   }
   public void actionPerformed (ActionEvent e)
   {
   }
   }

2. What is the name of the JTextArea?

3. How do you add something in a JTextArea?

4. This printing code for printing an array in a JTextArea is exactly the same as in applications (or non-Applets) except for one some section. What is it?

5. Change the JTextArea's background to yellow. Run your code. What is the line to change the JTextArea?

6. Put this code in Java. Run it.

import javax.swing.*;
   import java.awt.*;
   import java.awt.event.*;
   import java.applet.Applet;
public class ArraysUpdate extends Applet implements ActionListener
   {
   String list [] = new String [12];
   int end = 0;
   JTextArea box;
 public void init ()
   {
   list [0] = "Can of Tuna";
   list [1] = "Ketchup";
   list [2] = "Loaf of Bread";
   end = 3;
 box = new JTextArea (10, 10);
   for (int i = 0 ; i < end ; i++)
   box.append ((i + 1) + " " + list [i] + "\n");
   add (box);
 JButton addit = new JButton ("Add");
   addit.setActionCommand ("add");
   addit.addActionListener (this);
   add (addit);
   JButton delete = new JButton ("Delete");
   delete.setActionCommand ("delete");
   delete.addActionListener (this);
   add (delete);
   }
   public void actionPerformed (ActionEvent e)
   {
   if (e.getActionCommand ().equals ("add"))
   {
   String input = JOptionPane.showInputDialog ("Add what?");
   list [end] = input;
   end++;
   showStatus ("Added");
   }
   else if (e.getActionCommand ().equals ("delete"))
   {
   String input = JOptionPane.showInputDialog ("Delete what?");
   int pos = -1;
   for (int i = 0 ; i < end ; i++)
   {
   if (input.equals (list [i]))
   pos = i;
   }
   if (pos == -1)
   showStatus ("Not found");
   else
   {
   end--;
   for (int i = pos ; i < end ; i++)
   list [i] = list [i + 1];
   showStatus ("Deleted");
   }
 }
   box.setText ("");
   for (int i = 0 ; i < end ; i++)
   box.append ((i + 1) + " " + list [i] + "\n");
   }
   }

7. What is in the array initially?

8. How many spaces does the array have in total? How many are filled at the beginning?

9. The add code is exactly the same as the shopping list add code we did before. Outline the steps that happen.

10. What is the line of code to clear a JTextArea?

11. The delete code is exactly the same as the shopping list delete code that we did before. Outline the steps that happen. Do NOT cut and paste the code, describe what happens.