LevSelector.com New York
home > Java Events

Java Events

When something happens (user clicks the mouse, ...) - event object is generated and passed for processing.
Below described briefly:
   - types of events objects (also interfaces and adapters)
   - where processing occurs and how to program it
 
 
Java Events home - top of the page -

java.util.EventObject class
 \
 java.awt.AWTEvent (abstract class,  protected int id, protected boolean consumed)
     \
      java.awt.event  package
Classes - events Interfaces  Classes - adapters
ActionEvent (-)
AdjustmentEvent (-)
ComponentEvent (+)
 - ContainerEvent (+)
 - FocusEvent (+)
 - InputEvent
 --- MouseEvent (+2)
 --- KeyEvent (+)
 - PaintEvent 
 - WindowEvent (+)
HierarchyEvent (+)
InputMethodEvent (-)
InvocationEvent 
ItemEvent (-)
TextEvent (-)
ActionListener 
AdjustmentListener 
AWTEventListener 
ComponentListener 
ContainerListener 
FocusListener 
HierarchyBoundsListener 
HierarchyListener 
InputMethodListener 
ItemListener 
KeyListener 
MouseListener 
MouseMotionListener 
TextListener 
WindowListener

 

ComponentAdapter 
ContainerAdapter 
FocusAdapter 
HierarchyBoundsAdapter 
 
 

KeyAdapter
MouseAdapter 
MouseMotionAdapter 

WindowAdapter 

(+) - has an adapter,  (-) - no adapter
 
processing events home - top of the page -

first called the method processEvent(e) in the originating component (it may be overriden if necessary)

class:  java.awt.Component  (extends java.lang.Object)
method:  protected void processEvent (AWTEvent e)

All it does is determines what kind of event it is - and passes it to a more specific method in the same java.awt.Component class (they all protected void):

processComponentEvent(ComponentEvent e)
processFocusEvent(FocusEvent e)
processHierarchyBoundsEvent(HierarchyEvent e)
processHierarchyEvent(HierarchyEvent e)
processInputMethodEvent(InputMethodEvent e)
processKeyEvent(KeyEvent e)
processMouseEvent(MouseEvent e)
processMouseMotionEvent(MouseEvent e)

You can override any of those "process" methods.
Example:
public void processMouseEvent(MouseEvent e) {
  if (e.getID == MouseEvent.MOUSE_PRESSED) {
      // do some processing here
  }
  super.processMouseEvent(e)  // call this to make sure that listeners get the event too
}

Listener interfaces (~15 of them - see table above) - define methods to process different events.
Adapter classes - classes implementing listener interfaces with empty methods.

Example:
 
ActionListener interface - has only one method: actionPerformed(ActionEvent e).

First approach: make your whole application to implement the listener interface

public class test extends Frame implements ActionListener {

  // some stuff

  public void actionPerformed (ActionEvent e) {
     Object source = e.getSource( );
     if (source == button1) doButton1( );
     if (source == button2) doButton2( );
     if (source == textField1) processEntry(textField1.getText( ) );
  }

  button1.addActionListener(this);
  button2.addActionListener(this);
  textField1.addActionListener(this);
}

Second approach: use an anonymous inner class
Below we define an anonymous class which extends Object implements ActionListener.
The syntax for that is a shortcut (words "extends Object implements " are removed):

ActionListener al = new ActionListener( ) {
  public void actionPerformed (ActionEvent e) {
     Object source = e.getSource( );
     if (source == button1) doButton1( );
     ... // etc.
  }
};  // end of inner class creation

button1.addActionListener(al);
button2.addActionListener(al);
textField1.addActionListener(al);

Here we define the class is an

You can also define anonymous inner class in the function call itself:

button1.addActionListener (
  new ActionListener( ) {
     public void actionPerformed (ActionEvent e) {
         // do some processing here
     }
  }
);


 
More examples home - top of the page -

Item Event & ItemListener:
  AWT controls List, Choice, Checkbox  generate  ItemEvent  which is passed to ItemListener objects.
  Listener has one method:
     public void itemStateChanged( ItemEvent evt )
       evt.getSource - gives you the control
       evt.getID - compare with  ItemEvent.SELECTED  or  ItemEvent.DESELECTED
       getItem( )

AdjustmentEvent & AdjustmentListener:
   java.awt.Scrollbar
  Listener has one method:
     public void adjustmentValueChanged( AdjustmentEvent evt )
        getAdjustable - returns the object that generated the event
        getAdjustmentType
        getValue - returns int primitive

ComponentEvent & ComponentListener:
   public Component getComponent( )
   Listener has methods:
       componentHidden( )
       componentMoved( )
       componentResized( )
       componentShown( )
addComponentListener( 
  new ComponentAdapter( ) {
    public void componentResized( ComponentEvent e ) {
      System.out.println("Resized");
    }
  }
);

FocusEvent & FocusListener:
FocusAdapter has 2 methods: focusGained( ) & focusLost( )
getID can be compared with FocusEvent.FOCUS_GAINED  & FocusEvent.FOCUS_LOST

WindowEvent & WindowListener:
methods in the WindowAdapter: WindowActivated( ), windowOpened( ), windowClosed( ), windowClosing( ), windowIconified( ), windowDeiconified( ), windowDeactivated( )
NOTE: no WindowEvent on resizing - you should use ComponentListener for that

abstract InputEvent ==> MouseEvent & KeyEvent:
InputEvent class methods:
   consume( ), getModifiers( ), getWhen( ) (returns long time in ms)
   isAltDown( ), isAltGraphDown( ), isConsumed( ),
   isControlDown( ), isMetaDown( ), isShiftDown( )
InputEvent class constants (same as returned by getModifiers) : SHIFT_MASK, ALT_MASK, CTRL_MASK, META_MASK, ALT_GRAPH_MASK, BUTTON1_MASK, BUTTON2_MASK, BUTTON3_MASK

MouseEvent & MouseListener & MouseMotionListener:
MouseEvent class:
   getX( ), getY( ), getPoint( ), getClickCount( ), isPopupTrigger( ), translatePont( ), paramString( )

MouseMotionListener:
   mouseDragged( )
   mouseMoved( )

MouseListener:
   mouseClicked( ),  mouseEntered( ),  mouseExited( ),  mousePressed( ),  mouseReleased( )
 

KeyEvent & KeyListener:
keyPressed( KeyEvent e ), keyReleased( KeyEvent e ), keyTyped( KeyEvent e ),
public int getKeyCode( )
public char getKeyChar( )
setKeyChar( Char ch )
setModifieers( int mod )
textField1.addKeyListener( 
  new KeyAdapter( ) {
    public void keyTyped( KeyEvent e ) {
      e.setKeyChar ( Character.toUpperCase( e.getKeyChar( ) ));
    }
  }
);
All KeyListener objects registered with a TextField will see a new KeyEvent before a character is added to the text.
If a KeyListener calls a KeyEvent consume method - the character will not be added to the field, for example:
textField1.addKeyListener( 
  new KeyAdapter( ) {
    public void keyTyped( KeyEvent e ) {
      if( ! Character.isDigit( e.setKeyChar ) e.consume( );
    }
  }
);

TextEvent & TextEventListener:
  textValueChanged( TextEvent e )