Please send questions to st10@humboldt.edu .
/**
 * ManyJButtons2
 * 
 * a conversion of ManyButtons2 to now use Swing components
 * instead of AWT components.
 *
 * (a simple example of an array of objects, with a
 * more interesting layout manager for the buttons)
 *
 * RECOMMENDED SIZE: 400 x 650
 *
 * modified by: Sharon M. Tuttle
 * last modified: 2-12-01
 **/

// should no longer need Applet, and note that I HAD PROBLEMS
// ONCE when I left it in; I strongly suggest you NOT use it
// when using JApplet.
//import java.applet.Applet;

// we are still using AWT colors, event handlers, layout managers, etc.
import java.awt.*;
import java.awt.event.*;

// ...but of course, we now need Swing! (yes, JApplet is here;)
import javax.swing.*;

public class ManyJButtons2 extends JApplet implements ActionListener
{
        JLabel          instruction;
        JPanel          line1Panel, line2Panel, below1Panel;
        JButton[]       buttonSet;
        JTextField      numButtonsField;
        boolean         numButtonsChosen = false;
        JLabel          dummy;          // placeholder until buttons added

	// added to make applet more displayable
	Font 		biggerFont;

	// needed content panes, for the Swing containers
	Container	appletContP;		

        public void init()
        {
		biggerFont = new Font("Serif", Font.PLAIN, 30);			

		// remember one of the BIG changes: stuff goes on
		// the JApplet's content pane!
		appletContP = getContentPane();
				
		// now, throughout, if were doing for applet in general,
		// are likely doing it for the JApplet's content pane
		// (actually, JApplet's default layout is
		// BorderLayout, but I wanted to show how one does
		// set the layout manager...)
                appletContP.setLayout(new BorderLayout());

		// does THIS work?
                appletContP.setBackground(Color.yellow);
                System.out.println("ManyJButtons2, Version 1.0");

                // this will hold the labels and number-of-buttons
                // request
                line1Panel = new JPanel();
                line1Panel.setLayout(new FlowLayout());

                line2Panel = new JPanel();
                line2Panel.setLayout(new FlowLayout());

                // to get second "line" and rest, need below1Panel
                below1Panel = new JPanel();
                below1Panel.setLayout(new BorderLayout());

                JLabel ident = new JLabel("CIS 480 Spring 2001");
		ident.setFont(biggerFont);
                line1Panel.add(ident);

                JLabel title = new JLabel("Array of Buttons Example");
		title.setFont(biggerFont);
                line1Panel.add(title);

                instruction = new JLabel("# of buttons" +
                                        " desired:");
		instruction.setFont(biggerFont);
                line2Panel.add(instruction);

                numButtonsField = new JTextField(10);
                line2Panel.add(numButtonsField);
                numButtonsField.addActionListener(this);
		numButtonsField.setFont(biggerFont);
                numButtonsField.setText("");

                //--------
                // put lines 1 and 2 onto applet
                //--------

                // add line 1

                appletContP.add(line1Panel, BorderLayout.NORTH);

                // add line 2 and rest

                below1Panel.add(line2Panel, BorderLayout.NORTH);

                // dummy label, to hold place for button panel later
                dummy = new JLabel("             ");
                below1Panel.add(dummy, BorderLayout.CENTER);

                appletContP.add(below1Panel, BorderLayout.CENTER);
        }

        public void actionPerformed(ActionEvent e)
        {
                int numButtons;

                // only allowed to enter number of buttons once!
		// Note that, for JTextField, I can ALSO setEditable
		// to false, so user CANNOT type anything else into
		// numButtonsField
                if (numButtonsChosen == false)
                {
                        try
                        {
                           numButtons = Integer.parseInt(e.getActionCommand());

                           // is number of buttons reasonable? 
                           if ((numButtons > 0) && (numButtons <= 20))
                           {
                                numButtonsChosen = true;
								
				// if number of buttons is reasonable,
				// they can no longer edit numButtonsField
				numButtonsField.setEditable(false);

                                addButtonPanel(numButtons);
                           }

                           // what if numButtons is NOT reasonable?
                           else
                           {
                                numButtonsField.setText("Try > 0, <= 20");
                           }
                        } // end try block

                        catch(NumberFormatException ex)
                        {
                                numButtonsField.setText("Try an integer");
                        }
                } 

                // if reach here --- button should have been pressed!
                else
                {
                        // print button chosen in TextField
                        numButtonsField.setText(e.getActionCommand());
                }
        }

        private void addButtonPanel(int numBtns)
        {
                JPanel   buttonP;                // panel of buttons
                int      numCols = 4, numRows;   // numRows will be
                                                 // computed below
                int      horizSp=3, vertSp=3;    // for buttonP
                        
                buttonSet = new JButton[numBtns];

                //-------
                // change instruction label to reflect
                // new situation: button presses!
                //-------

                // this command will REMOVE a  component from
                // the applet --- won't SEE this without
                // validate(), below, however, to FORCE
                // applet re-layout.
                line2Panel.remove(instruction);
                instruction = new JLabel("was last button pressed");
				instruction.setFont(biggerFont);
                            
                // instruction is now being added AFTER the
                // two labels and textfield
                line2Panel.add(instruction);

                numButtonsField.setText("");

                // create a panel to hold the new buttons
                buttonP = new JPanel();
                buttonP.setBackground(Color.blue);

                // how many ROWS of 4 buttons each can we
                // have with this many buttons?
                // (ceiling: nearest "whole" number >= argument)
                numRows = (int) Math.ceil( ((double)numBtns) / numCols);

// debugging code -- feel free to delete/comment out as desired
//System.out.println("numBtns: " + numBtns);
//System.out.println("numCols: " + numCols);
//System.out.println("((double)numBtns) / numCols): " + 
//                   ((double)numBtns)/numCols );
//System.out.println("numRows: " + numRows);

                buttonP.setLayout(new GridLayout(numRows, numCols, 
                                                 horizSp, vertSp));

                // make buttons sensitive to user input,
                // and add them to buttonPanel
                for(int i = 0; i < buttonSet.length; i++)
                {
                        buttonSet[i] = new JButton("Button " + i);
						buttonSet[i].setFont(biggerFont);
                        buttonSet[i].addActionListener(this);
                        buttonP.add(buttonSet[i]);
                }

                // remove dummy, add buttonP in center of applet instead
                below1Panel.remove(dummy);
                below1Panel.add(buttonP, BorderLayout.CENTER);

                // force applet layout to be re-done
		// NOTICE that it is done to content pane!!
                appletContP.validate();
        }
}