/****************************************************/
/*String Tokens                                     */
/* m.booth                                          */
/* 2004 11 16 modified 11.17 Deital 2003            */
/* ics4m                                            */
/****************************************************/

//import section
import java.util.*; //event listener components
import java.awt.*;
import java.awt.event.*;
import javax.swing.*; //gui

public class Tokens extends JFrame
{
   //fields
   private JLabel promptLabel;
   private JTextField inputField;
   private JTextArea outputArea;

   // set up GUI and event handling
   public Tokens()
   {
      //construct JFrame with title bar
      super( "Testing Class StringTokenizer" );

      //create a container for output
      Container container = getContentPane();
      container.setLayout( new FlowLayout() );

      //add a label to the container
      promptLabel = new JLabel( "Enter a sentence and press Enter" );
      container.add( promptLabel );

      //add an input field and get input from user
      inputField = new JTextField( 20 );
      inputField.addActionListener(

         // anonymous inner class
         //listening for input
         new ActionListener()
         {

            // handle text field event
            public void actionPerformed( ActionEvent e )
            {
               //create a token and call input method
               StringTokenizer tokens =
                  new StringTokenizer( e.getActionCommand() );

               //output to output area #tokens and tokens
               outputArea.setText( "Number of elements: " +
                  tokens.countTokens() + "\nThe tokens are:\n" );

               //keep adding tokens until none left
               while ( tokens.hasMoreTokens() )
                  outputArea.append( tokens.nextToken() + "\n" );
            } //end of action performed method

         } // end anonymous inner class

      ); // end call to addActionListener

      container.add( inputField ); //add inputfield to container
      outputArea = new JTextArea( 10, 20 ); //set text area size
      outputArea.setEditable( false );//disable outputarea
      container.add( new JScrollPane( outputArea ) );
      setSize( 275, 240 );  // set the window size
      setVisible( true );   // show the window
   }

} // end class Tokens

