<<< JButton control | Index | ActionListener interface >>> |
Event-driven program expects various events in any order: user keystrokes, mouse clicks, minimizing windows, and so on
Source: a component where the event is generated
Listener: an object to be notified about the event and processing it
To accept event messages, create a class that implements ActionListener:
import java.awt.*; import java.awt.event.*; import javax.swing.*; import javax.swing.event.*; class MyButtonClickAction implements ActionListener { JButton mButton; public MyButtonClickAction( JButton button ) { mButton = button; } /** * Handles user clicks */ public void actionPerformed( ActionEvent event ) { mButton.setText( "Click!" ); } }//class MyButtonClickAction
Add action listener to the event source widget:
JButton button = new JButton( "Hello" ); button.addActionListener( new MyButtonClickAction( button ) );
<<< JButton control | Index | ActionListener interface >>> |