Event Classes in Java
Event Classes in Java
INTEGER CONSTANTS
CONSTRUCTORS
METHODS
ActionEvent class
ActionEvent(Object src, int type, String cmd, long when, int modifiers) Src -reference Type event type Cmd - command when time modifiers modifierkeys(alt,ctrl,shift,etc) are pressed
AdjustmentEvent Class
getAdjustable() object that generate the event getAdjustmentType() adjustment type such as decrement, increment, etc
ComponentEvent Class
ContainerEvent Class
COMPONENT_ADDED COMPONENT_REMOVED ContainerEvent(Component src, int type, Component comp) Comp component added or removed getContainer() container that caused the event getChild() component added or removed from the container
FocusEvent Class
FOCUS_GAINED FOCUS_LOST
FocusEvent(Component src, int type, Boolean temporaryFlag, Component other) temporaryFlag true if focus event is temporary other other component that lost or gained focus
InputEvent Class
isAltDown() to know the key is pressed or not isAltGraphDown() isControlDown() isMetaDown() isShiftDown()
ItemEvent class
DESELECTED SELECTED
ItemEvent(ItemSelectable src, int type, Object entry, int state) entry - item generated event state current state of the item
KeyEvent Class
KEY_PRESSED KEY_RELEASED KEY_TYPED VK_0 to VK_9 virtual key codes VK_A to VK_Z VK_ENTER VK_ESCAPE VK_CANCEL VK_UP VK_DOWN VK_LEFT VK_RIGHT VK_PAGE_DOWN VK_PAGE_UP VK_SHIFT VK_ALT VK_CONTROL MOUSE_CLICKED MOUSE_DRAGGED MOUSE_ENTERED MOUSE_EXITED MOUSE_MOVED MOUSE_PRESSED MOUSE_RELEASED MOUSE_WHEEL WHEEL_BLOCK_SCROLL - Page up or down
KeyEvent(Component src, int type, long when, int modifiers, int code, char ch) code virtual key code (VK_UP) ch character equivalent
MouseEvent class
MouseEvent(Component src, int type, long when, int modifiers, int x, int y, int clicks, Boolean triggers popup) x,y coordinates clicks - no. of mouse clicks triggers popup indicates if the event causes a popup menu MouseWheelEvent(Component src, int type, long when, int modifiers, int x, int y, int clicks, boolean
getX() getXY() getPoint() mouse coordinates getClickCount() isPopupTrigger() getButton() returns the button that caused the event(NO BUTTON, BUTTON1,BUTTON2,BUTTON3) getScrollType() getScrollAmount()
MouseWheelEvent class
triggers Popup, int scrollHow, int amount, int count) scrollHow block or unit scroll amount no. of units to scroll count no. of rotational units the wheel moved TextEvent(Object src, int type) - Signal to the listener to retrieve information from a text component
TextEvent class
TEXT_VALUE_CHANGED
WindowEvent class
WindowEvent(Windows src, int type, Window other, int fromState, int toState) other opposite window when a focus event occurs fromState prior state toState - new state
SOURCES OF EVENTS
Actual event Button pressed Item Selected or deselected Choice changed Item double clicked Item selected or deselected Item selected Checkable menu item is selected or deselected Scroll bar manipulated Character entered Window opened, closed or activated
Event type Action event Item event Item event Action event Item event Action event Item event
Menu Item
The delegation event model has two parts: sources and listeners. Listeners are created by implementing one or more of the interfaces defined by the java.awt.event package.
When an event occurs, the event source invokes the appropriate method defined by the listener and provides an event object as its argument.
INTERFACE
ActionListener
METHODS
actionPerformed( )
GENERAL FORMAT
void actionPerformed(ActionEvent ae)
AdjustmentListener
adjustmentValueChanged( )
ComponentListener
componentResized()
componentMoved()
componentShown()
componentHidden()
ContainerListener
componentAdded( )
componentRemoved( )
FocusListener
focusGained( ) focusLost( )
ItemListener
itemStateChanged( )
KeyListener
MouseListener
void mouseClicked(MouseEvent me) void mouseEntered(MouseEvent me) void mousePressed(MouseEvent me) void mouseExited(MouseEvent me) void mouseReleased(MouseEvent me)
MouseMotionListener
mouseDragged( ) mouseMoved( )
MouseWheelListener
mouseWheelMoved( )
TextListener
textChanged( )
WindowFocusListener
windowGainedFocus( ) windowLostFocus( )
WindowListener
windowActivated( )
windowClosed()
windowClosing()
windowDeactivated()
windowOpened()
Example for Handling Mouse Events // Demonstrate the mouse event handlers.
import java.awt.*; import java.awt.event.*; import java.applet.*; /* <applet code="MouseEvents" width=300 height=100> </applet> */ public class MouseEvents extends Applet implements MouseListener, MouseMotionListener { String msg = ""; int mouseX = 0, mouseY = 0; // coordinates of mouse public void init() { addMouseListener(this); addMouseMotionListener(this); } // Handle mouse clicked. public void mouseClicked(MouseEvent me) { // save coordinates mouseX = 0; mouseY = 10;
msg = "Mouse clicked."; repaint(); } // Handle mouse entered. public void mouseEntered(MouseEvent me) { // save coordinates mouseX = 0; mouseY = 10; msg = "Mouse entered."; repaint(); } // Handle mouse exited. public void mouseExited(MouseEvent me) { // save coordinates mouseX = 0; mouseY = 10; msg = "Mouse exited."; repaint(); } // Handle button pressed.
public void mousePressed(MouseEvent me) { // save coordinates mouseX = me.getX(); mouseY = me.getY(); msg = "Down"; repaint(); }
// Handle button released. public void mouseReleased(MouseEvent me) { // save coordinates mouseX = me.getX(); mouseY = me.getY(); msg = "Up"; repaint(); } // Handle mouse dragged. public void mouseDragged(MouseEvent me) { // save coordinates mouseX = me.getX();
mouseY = me.getY(); msg = "*"; showStatus("Dragging mouse at " + mouseX + ", " + mouseY); repaint(); } // Handle mouse moved. public void mouseMoved(MouseEvent me) { // show status showStatus("Moving mouse at " + me.getX() + ", " + me.getY()); } // Display msg in applet window at current X,Y location. public void paint(Graphics g) { g.drawString(msg, mouseX, mouseY); } }
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
/*
</applet>
*/
implements KeyListener {
addKeyListener(this);
showStatus("Key Down");
showStatus("Key Up");
msg += ke.getKeyChar();
repaint();
// Display keystrokes.
g.drawString(msg, X, Y);
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
/*
</applet>
*/
implements KeyListener {
addKeyListener(this);
showStatus("Key Down");
switch(key) {
case KeyEvent.VK_F1:
msg += "<F1>";
break;
case KeyEvent.VK_F2:
msg += "<F2>";
break;
case KeyEvent.VK_F3:
msg += "<F3>";
break;
case KeyEvent.VK_PAGE_DOWN:
msg += "<PgDn>";
break;
case KeyEvent.VK_PAGE_UP:
msg += "<PgUp>";
break;
case KeyEvent.VK_LEFT:
break;
case KeyEvent.VK_RIGHT:
break;
repaint();
showStatus("Key Up");
msg += ke.getKeyChar();
repaint();
// Display keystrokes.
g.drawString(msg, X, Y);
Adapter Classes An adapter class provides an empty implementation of all methods in an event listener interface. Adapter classes are useful when you want to receive and process only some of the events that are handled by a particular event listener interface.
You can define a new class to act as an event listener by extending one of the adapter classes .
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
/*
</applet>
*/
addMouseListener(new MyMouseAdapter(this));
addMouseMotionListener(new MyMouseMotionAdapter(this));
AdapterDemo adapterDemo;
this.adapterDemo = adapterDemo;
adapterDemo.showStatus("Mouse clicked");
AdapterDemo adapterDemo;
this.adapterDemo = adapterDemo;
adapterDemo.showStatus("Mouse dragged");
INNER CLASSES: An inner class is a class defined within other class. Inner classes can be used to simplify the code when using event adapter classes . eg. without inner class // This applet does NOT use an inner class.
import java.applet.*;
import java.awt.event.*;
/*
</applet>
*/
addMouseListener(new MyMouseAdapter(this));
MousePressedDemo mousePressedDemo;
this.mousePressedDemo = mousePressedDemo;
mousePressedDemo.showStatus("Mouse Pressed.");
import java.applet.*;
import java.awt.event.*;
/*
</applet>
*/
addMouseListener(new MyMouseAdapter());
showStatus("Mouse Pressed");