// ArrayTest.java by Dr. I. for CS 102 during Spring, 1999
// This program demonstrates a grid layout and an array of
//   text field "widgets"

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.*;

public class ArrayTest extends JFrame implements ActionListener  {
   public static final int MAX_ROW = 2;
   public static final int MAX_COLUMN = 3;
   public ArrayTest()  {
      setTitle("Board Test");  setSize(450,450);
      Container contentPane = getContentPane();
      JButton pressMe = new JButton("Press Me");
      pressMe.addActionListener(this);
      JPanel panel = new JPanel();
      panel.setLayout(new GridLayout(MAX_ROW,MAX_COLUMN));
      board = new JTextField[MAX_ROW][MAX_COLUMN];  // 2-D array of handles
      for (int row = 0; row < MAX_ROW; ++row)  {  // create array elements
         for (int column = 0; column < MAX_COLUMN; ++column)  {
            board[row][column] = new JTextField(10);
            board[row][column].addActionListener(this);
            panel.add(board[row][column]);
         }
      }
      contentPane.setLayout(new BorderLayout());
      contentPane.add(panel, "Center");
      contentPane.add(pressMe, "South");
   }
   public void actionPerformed(ActionEvent e)  {
      Object source = e.getSource();
      if (source instanceof JButton)  {
         board[0][1].setText(board[0][0].getText());
      }  else  {
         System.out.println(((JTextField)source).getText());
      }
   }
   public static void main(String[] args)  {
      ArrayTest bt = new ArrayTest();
      bt.show();
      
      // to close application window
      bt.addWindowListener(
         new WindowAdapter() {
            public void windowClosing(WindowEvent e) {
               System.exit(0);
            }
         }
      );
   }
   private JTextField[][] board;
}

