import javax.swing.*;  // JFrame
import java.awt.*; // container
import java.awt.event.*; // handlers
public class ComboTest extends JFrame implements ActionListener, ItemListener
{
public JTextField writeBox;
public int times = 1;
public JButton exitButton;
public JComboBox myCombo;
public JButton writeButton;

public ComboTest()
   {
   setSize( 275, 100 );
   setDefaultCloseOperation(EXIT_ON_CLOSE);
   setLayout( new FlowLayout( ) ); 

   exitButton = new JButton( "Exit" );
   add( exitButton );
   exitButton.addActionListener( this );

   String names[ ] = {"stay", "leave", "ignore"};
   myCombo = new JComboBox( names );
   add( myCombo);   
   myCombo.addItemListener( this );

   writeBox = new JTextField( "Hello" );
   add( writeBox );
   writeBox.addActionListener( this );
   
   writeButton = new JButton( "Write" );
   add( writeButton );   
   writeButton.addActionListener( this );

   setVisible(true);
   }
	 
public static void main (String [] args)
   {
   ComboTest app = new ComboTest();
   }
   
   public void actionPerformed ( ActionEvent e ) 
      {
  		if (e.getActionCommand().equals("Exit"))
		   {
			System.exit(0); 
			} 
		if (e.getActionCommand().equals("Write"))
		   {
			System.out.println( writeBox.getText() );
			} 	 	 
      } // end actionPerformed method

   public void itemStateChanged( ItemEvent x )
      {
		System.out.println("times: " + times);
		// handle every OTHER action (that is, selection not DEselection)
		if (times == 1)
		   {
			times++;
			}
   	else
		   {
			times = 1;
			System.out.println( x.getItem( ) );
			writeBox.setText( x.getItem().toString() );
			}    
		}//  end ItemStateChanged
   
} // end class

