import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JLabel; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.Component; import java.awt.Container; public class MyButtonListener implements ActionListener { // Different message appears in the Frame depending on // buttom pressesed. Identify button using the // "actionCommand" public void actionPerformed(ActionEvent e) { JButton aButton = (JButton) e.getSource(); MyFrame aFrame = (MyFrame) aButton.getRootPane().getParent(); String temp = aButton.getActionCommand(); if(temp.equalsIgnoreCase(MyFrame.B1IDENTIFIER)) aFrame.setTitle("Button 1 pressed"); else if(temp.equalsIgnoreCase(MyFrame.B2IDENTIFIER)) aFrame.setTitle("Button 2 pressed"); } }