Button Array

 

Basic Points

Example 1: 2D Array

 

 

import java.awt.*;

import java.awt.event.*;

import java.applet.*;

 

/*A "2D" array of Buttons

- make a 1D array of buttons, put it in a grid

- make another array to track things in it

- translate out the tracking array's values into the 1D array when "printing"

*/

 

public class Buttons2DArray extends Applet implements ActionListener

{

    //applet member data

    Button [] a; //1D array to simulate a 2D one

    int row = 10;

    int col = 10;

    int total = row * col;

    Color tracker [] []; //2D array to track colours in 1D button array

   

 

    public void init ()

    {

        setLayout (new GridLayout (row, col, 0, 0));

 

        //initialize tracking array to have all black backgrounds

        tracker = new Color [row] [col];

        for (int i = 0 ; i < row ; i++)

        {

            for (int j = 0 ; j < col ; j++)

            {

                tracker [i] [j] = Color.black;

            }

        }

               

        //declare a new array of buttons

       a = new Button [total];

 

        //initialize each of the buttons in the array

        //with an empty label

        for (int nNum = 0 ; nNum < total ; nNum++)

        {

            a [nNum] = new Button ("");

            add (a [nNum]);

            //each button will have an action listener

            a [nNum].addActionListener (this);

            a [nNum].setBackground (Color.black);

            //each button will send a message with its number

            a [nNum].setActionCommand ("" + nNum);

        }

 

 

    }

 

 

    public void actionPerformed (ActionEvent e)

    { //To handle the button array - invoked when button is clicked

 

        //get the number of the button from getActionCommand

        //convert it to an int and store it in i

        int pos = Integer.parseInt (e.getActionCommand ());

 

        //find i and j

       int i = pos / row;

        int j = pos % row;

 

        //set tracker to have a new background colour

        if (tracker [i] [j] == Color.red)

            tracker [i] [j] = Color.black;

        else

            tracker [i] [j] = Color.red;

 

        //copy tracker's colours out into the button array

        int move = 0;

        for (i = 0 ; i < row ; i++)

        {

            for (j = 0 ; j < col ; j++)

            {

                a [move].setBackground (tracker [i] [j]);

                move++;

            }

        }

 

    }

}

 

 

Example 2: Typewriter

 

 

import java.awt.*;

import java.awt.event.*;

import javax.swing.*;

 

/**

 * Assignment - Typewriter

 * @author Ms. Gorski

 * @date July 23, 2002

 * Simulates a typewriter

 * Shows how to use an array of buttons

 */

 

 

public class Typewriter extends JPanel implements ActionListener

{

 

    JButton alpha [] = new JButton [26];

    JTextField word;

    JButton clear, space;

 

 

    public Typewriter ()

    {

        super (new BorderLayout ());

        setBackground (Color.white);

        //sets up the output label

        word = new JTextField ("                                                   ");

        word.setFont (new Font ("Courier", Font.BOLD, 18));

        word.setForeground (Color.blue);

 

        //sets up the buttons

        clear = new JButton ("Clear");

        clear.setActionCommand ("clear");

        clear.addActionListener (this);

        space = new JButton ("Space");

        space.setActionCommand (" ");

        space.addActionListener (this);

 

        //the alphabet set up

        JPanel alp = new JPanel (new FlowLayout ());

        JPanel alp2 = new JPanel (new FlowLayout ());

        JPanel alpwrap = new JPanel (new GridLayout (2, 1));

        //Create the alphabet

        for (int i = 0 ; i < 26 ; i++)

        {

            char a [] = new char [1];

            a [0] = (char) (i + 97);

            String x = new String (a);

            alpha [i] = new JButton (x);

            alpha [i].setActionCommand (x);

            alpha [i].addActionListener (this);

            if (i < 14)

                alp.add (alpha [i]);

            else

                alp2.add (alpha [i]);

        }

        alp2.add (space);

        alp2.add (clear);

        alp.setBackground (Color.white);

        alp2.setBackground (Color.white);

        alpwrap.add (alp);

        alpwrap.add (alp2);

 

        //make the layout

        add (word, "North");

        add (alpwrap, "South");

 

        //set up a space around the outside of the screen

        setBorder (BorderFactory.createEmptyBorder (20, 20, 20, 20));

 

    }

 

 

    /** Listens to the radio buttons and buttons.

    */

    public void actionPerformed (ActionEvent e)

    {

 

        if (e.getActionCommand ().equals ("clear"))

        { //user chooses clear - reset display

 

            word.setText ("");

 

        }

        else

        { //user clicked on a button

            //find the letter

            //add it to the textfield variable

            String a = word.getText();

            a=a.trim();

            word.setText (a + e.getActionCommand ());

 

        }

 

    }

 

 

    /**

    Directly from: http://java.sun.com/docs/books/tutorial/uiswing/components/example-1dot4/index.html#RadioButtonDemo

     */

    public static void main (String [] args)

    {

        //Make sure we have nice window decorations.

        JFrame.setDefaultLookAndFeelDecorated (true);

 

        //Create and set up the window.

        JFrame frame = new JFrame ("Typewriter");

        frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);

 

        //Create and set up the content pane.

        JComponent newContentPane = new Typewriter ();

        newContentPane.setOpaque (true); //content panes must be opaque

        frame.setContentPane (newContentPane);

 

        //Display the window.

        frame.pack ();

        frame.setVisible (true);

    }

}