Delegation Event Model
Sun Microsystems Discussion
http://java.sun.com/j2se/1.3/docs/guide/awt/designspec/events.html
Introduction
Graphical user interfaces (GUIs) are event
driven - they generate events when the user interacts with them by clicking etc.
GUIs have event handlers associated with them - explained below.
When the interaction occurs, an event is
automatically sent to the program for processing. The event information is
stored in an object that inherits from AWTEvent. The hierarchy of classes
involved is illustrated below.
Note that the Swing component uses these
event types. Swing has added new ones that
are defined in javax.swing.event.
Event
Classes of awt.event
Listener Interfaces of awt.event
An event listener for a GUI is an object of a
class that implements one or more of the event-listener interfaces from package
java.awt.event and package javax.swing.event. Many of the event listeners are
common to these two packages.
The Process
In order to process one of these user
interface events, the programmer must perform two important tasks.
Register an event listener
Implement an event handler
An event listener object then "listens" for
specific types of event generated in the object or by other objects (usually
GUIs) in the program.
An event handler is a method that is
automatically called in response to a particular type of event.
Each event listener interface specifies one
or more event handling methods that must be defined in the class that implements
the event-listener interface.
Delegation Event Model means that the
processing of an event is delegated to a particular object in the program.
Example
// Demonstrating the JTextField class and the
Delegation Event Model
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class TextFieldTest extends JFrame
{
private JTextField text1, text2,
text3;
private JPasswordField password;
public TextFieldTest()
{
super(
"Testing JTextField and JPasswordField" );
Container c =
getContentPane();
c.setLayout(
new FlowLayout() );
// construct
textfield with default sizing
text1 = new
JTextField( 10 );
//2. Is 10 width or height or neither?
c.add( text1
);
// construct
textfield with default text
text2 = new
JTextField( "Enter text here" );
c.add( text2
);
// construct
textfield with default text and 20 visible elements and no event handler
text3 = new
JTextField( "Uneditable text field", 20 );
text3.setEditable( false );
c.add( text3
);
// construct
textfield with default text
password = new
JPasswordField( "Hidden text" );
c.add(
password );
TextFieldHandler handler = new TextFieldHandler();
text1.addActionListener( handler );
text2.addActionListener( handler );
text3.addActionListener( handler );
password.addActionListener( handler ); //3. What is handler?
setSize( 325,
100 );
show();
}
public static void main( String
args[] )
{
TextFieldTest
app = new TextFieldTest();
app.addWindowListener(new WindowAdapter()
{
public void windowClosing( WindowEvent e )
{
System.exit( 0 );
//4. What is the purpose of this code?
}
}
);
}
// inner class for event
handling
private class TextFieldHandler
implements ActionListener
{
public void
actionPerformed( ActionEvent e )
//5. What is ActionEvent?
{
String s = "";
if ( e.getSource() == text1 )
s = "text1: " + e.getActionCommand();
else if ( e.getSource() == text2 )
s = "text2: " + e.getActionCommand();
else if ( e.getSource() == text3 )
s = "text3: " + e.getActionCommand();
else if ( e.getSource() == password )
{
JPasswordField pwd =
(JPasswordField) e.getSource();
s = "password: " +
new String( pwd.getPassword() );
}
JOptionPane.showMessageDialog( null, s );
}
}
}