[ Back | Previous | Next ]

Using an Actionadapter to control the events on a component.

Package:
java.awt.event.*
Product:
JDK
Release:
1.1.x
Related Links:
ActionEvent
KeyEvent
MouseEvent
WindowEvent
Comment:

The action adapter is not a part of the java.awt.event classes (windowsadapter is). So here is the source for the ActionAdapter. Once again for structual programming and neatly source these adapters are very nice to use. The main advantage is that you can instantly adapt the listener to an event on an individual level. The source for the ActionAdapter.

    //
    // ActionAdapter.java
    //
    package java.awt.event;   
    import java.awt.event.*;

public abstract class ActionAdapter extends Object implements ActionListener { public ActionAdapter() { }

public abstract void actionPerformed(ActionEvent ae); }

Example: In this example we implement an open button with a actionlistener which referes to an action adapter that handles how the action should be performed by means of the actionPerformed() method. event listener ... adapter class ...

import java.awt.*;   
import java.awt.event.*;
class GUI extends Frame {
        public GUI() {
                // change backgroundcolor
                setBackground( Color.white);        
                // Building search GUI.
                add( sopen = new Button( "Open" ));     
                sopen.addActionListener(    
                        new ActionAdapter() {
                                public void actionPerformed(ActionEvent e) {  
                                                System.out.println("Open");  
                                                        open();
                                }
                        }  
                );              
                searchquery = new TextField( 20 );
                searchresults = new TextArea( 4, 20 );

add( searchquery ); add( searchresults ); }

1