import java.io.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class gridTestSer extends JFrame implements Serializable
{
// define all components outside of methods, so e.getSource()
// can identify them in the ActionPerformed method
JTextField writeBox;
JButton exitButton;
JComboBox myCombo;
JButton writeButton;

String lastUsed =" ";

JButton AButton;
JButton BButton;
JButton CButton;

JRadioButton Rad1Button;
JRadioButton Rad2Button;
JRadioButton Rad3Button;
String names[ ] = {"stay", "leave", "ignore"};

public gridTestSer()
{
// move stuff out of constructor
}

public void startUp()
   {
   setSize( 500, 500 );
   setDefaultCloseOperation(EXIT_ON_CLOSE);

   Container c = getContentPane();
   c.setLayout( new GridLayout( 3,6 ) ); // rows are dominant 
   
   // create an icon out of local file "Exit.gif"
   ImageIcon exitSign = new ImageIcon("Exit.gif");
   exitButton = new JButton( exitSign );   
      
   // create more components
   writeBox = new JTextField( 200 );
   writeButton = new JButton( "Write" );
   myCombo = new JComboBox( names );
   AButton = new JButton( "A" );
   BButton = new JButton( "B" );
   CButton = new JButton( "C" );
 
   // fancy radio buttons  
   Rad1Button = new JRadioButton( "Yes" );
   Rad1Button.setForeground( Color.BLACK );
   Rad1Button.setBackground( Color.GREEN );
   Rad1Button.setFont( new Font( "Dialog", Font.PLAIN, 30 ) );
   
   Rad2Button = new JRadioButton("No"); 
   Rad2Button.setForeground( Color.WHITE );
   Rad2Button.setBackground( Color.RED );
   Rad2Button.setFont( new Font( "Dialog", Font.PLAIN, 30 ) );

   Rad3Button = new JRadioButton("Maybe"); 
   Rad3Button.setForeground( Color.GREEN );
   Rad3Button.setBackground( Color.YELLOW );
   Rad3Button.setFont( new Font( "Dialog", Font.PLAIN, 30 ) );
   
   // group together radio buttons (allows pressing of one only)
   ButtonGroup group = new ButtonGroup();
   group.add(Rad1Button);
   group.add(Rad2Button);
   group.add(Rad3Button);
   
   // put the radio buttons in a mini-JFrame
   JPanel buttonPanel = new JPanel();
   buttonPanel.setLayout(new BorderLayout());
   buttonPanel.add( Rad1Button, BorderLayout.NORTH );
   buttonPanel.add( Rad2Button, BorderLayout.CENTER );
   buttonPanel.add( Rad3Button, BorderLayout.SOUTH );
     
   // add everything to the container
   c.add( writeBox  );   
   c.add( AButton );
   c.add( BButton  );
   c.add( CButton  );
   c.add( myCombo );
   c.add( writeButton );
   c.add(buttonPanel);
   c.add( exitButton);
   
   // create handlers
   eventClass handler = new eventClass( );

   // add components to appropriate handlers   
   writeButton.addActionListener( handler );
   writeBox.addActionListener( handler );
   exitButton.addActionListener( handler );
   AButton.addActionListener( handler );
   BButton.addActionListener( handler );
   CButton.addActionListener( handler );
   Rad1Button.addActionListener( handler );
   Rad2Button.addActionListener( handler );
   Rad3Button.addActionListener( handler );
   
   myCombo.addActionListener(handler);
   
   setVisible(true);
   }

/*	 
public static void main (String [] args)
   {
   gridTestSer app = new gridTestSer();
   }
*/

// actions   
private class eventClass implements ActionListener 
   {
   public void actionPerformed ( ActionEvent e ) 
      {
      // use e.getActionCommand( ); to print button label
      System.out.println("Action: " + e.getActionCommand( ));
      if (e.getSource() == exitButton)
         {
		 System.out.println("Exiting");
		 System.exit(0); 
		 } 
		 
	  else if (e.getSource() == writeButton)
	     {
		 System.out.println( "Text in box: " + writeBox.getText() );
		 lastUsed = writeBox.getText();
		 names[2]= lastUsed;
         myCombo.addItem(lastUsed);
         
		 } 	 	 
	  else if (e.getSource() == AButton)
	     {
		 writeBox.setText(writeBox.getText() + "A");
	     } 	 	 
	  else if (e.getSource() == BButton)
	     {

        try{
		   String textInBox = writeBox.getText();
		   int textBoxLength = textInBox.length();
		   writeBox.setText( textInBox.substring(0, textBoxLength - 1) );
           }
		 catch (Exception err)
		   {
		   // this space intentionally left blank
		   }	   
	     } 	 	 
	  else if (e.getSource() == myCombo)
	     {
		 writeBox.setText("ComboItem: " + myCombo.getSelectedItem());
	     } 	 	 
	  else if (e.getSource() == CButton)
	     {
		 writeBox.setText(writeBox.getText() + "C");
	     } 	 	 
	 else if (e.getSource() == writeBox)
	     {
		 System.out.println( String.valueOf(  writeBox.getColumns()  ) );
	     } 	 	 
	 
      } // end actionPerformed method
   } // end private class

   
} // end class

