UNB/ CS/ David Bremner/ teaching/ java/ KeyPad.java
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import javax.swing.JButton;
import javax.swing.JPanel;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

/**
   A component that lets the user enter a number, using 
   a button pad labeled with digits
*/

public class KeyPad extends JPanel implements ActionListener {  

    private KeyPadClient client;

    // Implementation of java.awt.event.ActionListener

    /**
     * Callback routine to process events from the JButtons.
     *
     * @param actionEvent an <code>ActionEvent</code> value
     */

    public void actionPerformed(ActionEvent actionEvent) {
        String text=actionEvent.getActionCommand();

        client.keypressCallback(text);
    }

    public KeyPad(String [][] labels){
        int height = labels.length;
        int width  = labels[0].length;
        setLayout(new GridLayout(height,width));

        for (int i=0; i< height; i++){
            for (int j=0; j<width; j++){
                JButton button=new JButton(labels[i][j]);
                button.addActionListener(this);
                add(button);
            }
        }
    }



    public void setClient(KeyPadClient argClient){
        client=argClient;
    }   
       
    public KeyPad(String [][] labels, 
                  KeyPadClient argClient)
    {  
        this(labels);

        setClient(argClient);
    }


}

//