Java provides event adapter class simplify to the creation of event handlers.
An adapter class provides an empty implementation of all methods in an event listener interface.
To receive and process only some of the events that are handled by a particular event listener interface, use the Adapter classes.
The following table lists several commonly used adapter classes in java.awt.event.
Adapter Class | Listener Interface |
---|---|
ComponentAdapter | ComponentListener |
ContainerAdapter | ContainerListener |
FocusAdapter | FocusListener |
KeyAdapter | KeyListener |
MouseAdapter | MouseListener and MouseMotionListener and MouseWheelListener |
MouseMotionAdapter | MouseMotionListener |
WindowAdapter | WindowListener, WindowFocusListener, and WindowStateListener |
// Demonstrate the key event handlers. import java.awt.event.MouseAdapter; import java.awt.event.MouseEvent; import java.awt.event.MouseMotionAdapter; import javax.swing.JFrame; import javax.swing.JScrollPane; import javax.swing.JTextArea; import javax.swing.SwingUtilities; class SimpleKey extends JTextArea { public SimpleKey() { addMouseListener(new MyMouseAdapter(this)); addMouseMotionListener(new MyMouseMotionAdapter(this)); }//from w w w . ja v a2 s.c o m } class MyMouseAdapter extends MouseAdapter { SimpleKey adapterDemo; public MyMouseAdapter(SimpleKey adapterDemo) { this.adapterDemo = adapterDemo; } // Handle mouse clicked. public void mouseClicked(MouseEvent me) { adapterDemo.append("Mouse clicked"); } } class MyMouseMotionAdapter extends MouseMotionAdapter { SimpleKey adapterDemo; public MyMouseMotionAdapter(SimpleKey adapterDemo) { this.adapterDemo = adapterDemo; } // Handle mouse dragged. public void mouseDragged(MouseEvent me) { adapterDemo.append("Mouse dragged"); } } public class Main { public static void main(String args[]) { // Create the frame on the event dispatching thread. SwingUtilities.invokeLater(new Runnable() { public void run() { // Create a new JFrame container. JFrame jfrm = new JFrame("java2s.com"); // Give the frame an initial size. jfrm.setSize(220, 200); // Terminate the program when the user closes the application. jfrm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // Add the label to the content pane. jfrm.add(new JScrollPane(new SimpleKey())); // Display the frame. jfrm.setVisible(true); } }); } }
Java provides adapter classes for each listener interface with more than one method.
For example, the MouseAdapter class implements the MouseListener interface.
An adapter class implements empty versions of all its interface's methods.
A listener class may be created quickly by extending an adapter class and overriding only the methods of interest.
import java.awt.FlowLayout; import java.awt.event.KeyAdapter; import java.awt.event.KeyEvent; import javax.swing.JFrame; import javax.swing.JTextField; public class Main extends JFrame { JTextField t = new JTextField(15); Main() {/*from w w w .j a va 2 s .c o m*/ setSize(180, 80); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setLayout(new FlowLayout()); add(t); t.addKeyListener(new CaseChanger()); setVisible(true); } class CaseChanger extends KeyAdapter { public void keyReleased(KeyEvent e) { if (e.getSource() instanceof JTextField) { JTextField t = (JTextField) e.getSource(); int cp = t.getCaretPosition(); t.setText(t.getText().toUpperCase()); t.setCaretPosition(cp); } } } public static void main(String args[]) { new Main(); } }