LevSelector.com |
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
KeyAdapter
WindowAdapter |
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) {
button1.addActionListener(this);
Second approach: use an anonymous inner class
ActionListener al = new ActionListener( ) {
button1.addActionListener(al);
Here we define the class is an You can also define anonymous inner class in the function call itself: button1.addActionListener (
|
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( ) )); } } ); |
textField1.addKeyListener(
new KeyAdapter( ) { public void keyTyped( KeyEvent e ) { if( ! Character.isDigit( e.setKeyChar ) e.consume( ); } } ); |
TextEvent & TextEventListener:
textValueChanged( TextEvent e )