Java Programming/Applets/ActionListener
From Wikibooks, the open-content textbooks collection
ActionListener is an interface that could be implemented in order to determine how certain event should be handled. When implementing an interface, all methods in that interface should be implemented, ActionListener interface has one method to implement named actionPerformed.
The following example shows how to implement ActionListener:
import javax.swing.*; import java.awt.*; import java.awt.event.*; class ActionDemo extends JFrame implements ActionListener{ public ActionDemo(){ setDefaultCloseOperation(EXIT_ON_CLOSE); setSize(300, 300); getContentPane().setLayout(new FlowLayout()); JButton b = new JButton("Click me"); getContentPane().add(b); b.addActionListener(this); } public void actionPerformed(ActionEvent e){ JOptionPane.showMessageDialog(this, "Hi !!!"); } public static void main(String[] args){ new ActionDemo().setVisible(true); } }
When you compile and run the above code, a message will appear when you click on the button.