import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;


/**

   This program demonstrates how to use getSource() to find out
   what component is the source of an event.  This is useful if
   you attach the same listener to several components.

*/

public class GetSourceTester
{  
    public static void main(String[] args)
    {  
	JFrame frame = new JFrame();
        frame.setLayout(new FlowLayout());
	final JButton button1 = new JButton("yes");
	frame.add(button1);
	final JButton button2 = new JButton("no");
	frame.add(button2);


      class ClickListener implements ActionListener
      {
	  public void actionPerformed(ActionEvent event)
	  {
	      if(event.getSource() == button1)
	      {   System.out.println("You clicked yes");
	      }
	      else
	      {   // event.getSource() == button2
		  System.out.println("You clicked no");
	      }
              // another way
              System.out.println("I repeat: you clicked " +
                                 ((JButton)event.getSource()).getText());        
	  }            
      };
     
      ActionListener listener = new ClickListener();
      button1.addActionListener(listener);
      button2.addActionListener(listener);

      frame.setSize(FRAME_WIDTH, FRAME_HEIGHT);
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      frame.setVisible(true);
    }

    private static final int FRAME_WIDTH = 200;
    private static final int FRAME_HEIGHT = 60;
}

