Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (1 vote)
3K views

Event Classes in Java

The document describes Java event classes and interfaces. It lists common event types like mouse, keyboard, window etc and their corresponding event classes. It provides details on event class constructors and methods to get event properties. It also describes listener interfaces that need to be implemented to receive events and how the delegation event model works by registering listeners on sources.

Uploaded by

Chidambaram
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (1 vote)
3K views

Event Classes in Java

The document describes Java event classes and interfaces. It lists common event types like mouse, keyboard, window etc and their corresponding event classes. It provides details on event class constructors and methods to get event properties. It also describes listener interfaces that need to be implemented to receive events and how the delegation event model works by registering listeners on sources.

Uploaded by

Chidambaram
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 27

EVENT CLASS NAME

INTEGER CONSTANTS

CONSTRUCTORS

METHODS

ActionEvent class

ALT_MASK CTRL_MASK META_MASK SHIFT_MASK ACTION_PERFORMED

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

getActionCommand() command name getModifiers() modifiers keys pressed getWhen() - time

AdjustmentEvent Class

BLOCK_DECREMENT BLOCK_INCREMENT TRACK UNIT_DECREMENT UNIT_INCREMENT ADJUSTMENT_VALUE_CHANGED

AdjustmentEvent(Adjustable src, int id, int type, int data)

getAdjustable() object that generate the event getAdjustmentType() adjustment type such as decrement, increment, etc

ComponentEvent Class

COMPONENT_HIDDEN COMPONENT_MOVED COMPONENT_RESIZED COMPONENT_SHOWN

ComponentEvent(Component src, int type)

getComponent() component that generated the event

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

getOppositeComponent() opposite component is returned isTemporary() true if change is temporary

InputEvent Class

ALT_DOWN_MASK ALT_GRAPH_DOWN_MASK BUTTON1_DOWN_MASK BUTTON2_DOWN_MASK BUTTON3_DOWN_MASK CTRL_DOWN_MASK META_DOWN_MASK SHIFT_DOWN_MASK

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

getItem() reference to the item that generated the event getItemSelectable() -

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

getKeyChar() returns character entered getKeyCode() returns key code

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

WHEEL_UNIT_SCROLL - Line up or down

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

WINDOW_ACTIVATED WINDOW_CLOSED WINDOW_CLOSING WINDOW_DEACTIVATED WINDOW_GAINED_FOCUS WINDOW_LOST_FOCUS WINDOW_OPENED WINDOW_STATE_CHANGED

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

getWindow() getOppositeWindow() getOldState() getNewState()

SOURCES OF EVENTS

Source Button Checkbox Choice List

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

Scroll bar Text components Window

Adjustment event Text event Window events

Event Listener Interfaces


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( )

void adjustmentValueChanged(AdjustmentEvent ae)

ComponentListener

componentResized()

void componentResized(ComponentEvent ce)

componentMoved()

void componentMoved(ComponentEvent ce)

componentShown()

void componentShown(ComponentEvent ce)

componentHidden()

void componentHidden(ComponentEvent ce)

ContainerListener

componentAdded( )

void componentAdded(ContainerEvent ce)

componentRemoved( )

void componentRemoved(ContainerEvent ce)

FocusListener

focusGained( ) focusLost( )

void focusGained(FocusEvent fe) void focusLost(FocusEvent fe)

ItemListener

itemStateChanged( )

void itemStateChanged(ItemEvent ie)

KeyListener

keyPressed( ) keyReleased( ) keyTyped( )

void keyPressed(KeyEvent ke) void keyReleased(KeyEvent ke) void keyTyped(KeyEvent ke)

MouseListener

mouseClicked( ) mouseEntered( ) mousePressed( ) mouseExited( ) mouseReleased( )

void mouseClicked(MouseEvent me) void mouseEntered(MouseEvent me) void mousePressed(MouseEvent me) void mouseExited(MouseEvent me) void mouseReleased(MouseEvent me)

MouseMotionListener

mouseDragged( ) mouseMoved( )

void mouseDragged(MouseEvent me) void mouseMoved(MouseEvent me)

MouseWheelListener

mouseWheelMoved( )

void mouseWheelMoved(MouseWheelEvent mwe)

TextListener

textChanged( )

void textChanged(TextEvent te)

WindowFocusListener

windowGainedFocus( ) windowLostFocus( )

void windowGainedFocus(WindowEvent we) void windowLostFocus(WindowEvent we)

WindowListener

windowActivated( )

void windowActivated(WindowEvent we)

windowClosed()

void windowClosed(WindowEvent we)

windowClosing()

void windowClosing(WindowEvent we)

windowDeactivated()

void windowDeactivated(WindowEvent we)

windowOpened()

void windowOpened(WindowEvent we)

Using the Delegation Event Model


Follow these two steps: 1. Implement the appropriate interface in the listener so that it will receive the type of event desired.

2. Implement code to register and unregister the listener.

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); } }

Handling Keyboard Events Eg:


/ Demonstrate the key event handlers.

import java.awt.*;

import java.awt.event.*;

import java.applet.*;

/*

<applet code="SimpleKey" width=300 height=100>

</applet>

*/

public class SimpleKey extends Applet

implements KeyListener {

String msg = "";

int X = 10, Y = 20; // output coordinates

public void init() {

addKeyListener(this);

requestFocus(); // request input focus

public void keyPressed(KeyEvent ke) {

showStatus("Key Down");

public void keyReleased(KeyEvent ke) {

showStatus("Key Up");

public void keyTyped(KeyEvent ke) {

msg += ke.getKeyChar();

repaint();

// Display keystrokes.

public void paint(Graphics g) {

g.drawString(msg, X, Y);

Another example: // Demonstrate some virtual key codes.

import java.awt.*;

import java.awt.event.*;

import java.applet.*;

/*

<applet code="KeyEvents" width=300 height=100>

</applet>

*/

public class KeyEvents extends Applet

implements KeyListener {

String msg = "";

int X = 10, Y = 20; // output coordinates

public void init() {

addKeyListener(this);

requestFocus(); // request input focus

public void keyPressed(KeyEvent ke) {

showStatus("Key Down");

int key = ke.getKeyCode();

switch(key) {

case KeyEvent.VK_F1:

msg += "<F1>";

break;

case KeyEvent.VK_F2:

msg += "<F2>";

break;

case KeyEvent.VK_F3:

msg += "<F3>";

THE JAVA LIBRARY

break;

case KeyEvent.VK_PAGE_DOWN:

msg += "<PgDn>";

break;

case KeyEvent.VK_PAGE_UP:

msg += "<PgUp>";

break;

case KeyEvent.VK_LEFT:

msg += "<Left Arrow>";

break;

case KeyEvent.VK_RIGHT:

msg += "<Right Arrow>";

break;

repaint();

public void keyReleased(KeyEvent ke) {

showStatus("Key Up");

public void keyTyped(KeyEvent ke) {

msg += ke.getKeyChar();

repaint();

// Display keystrokes.

public void paint(Graphics g) {

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 .

And implement only those events in which you are interested.

example // Demonstrate an adapter.

import java.awt.*;

import java.awt.event.*;

import java.applet.*;

/*

<applet code="AdapterDemo" width=300 height=100>

</applet>

*/

public class AdapterDemo extends Applet {

public void init() {

addMouseListener(new MyMouseAdapter(this));

addMouseMotionListener(new MyMouseMotionAdapter(this));

class MyMouseAdapter extends MouseAdapter {

AdapterDemo adapterDemo;

public MyMouseAdapter(AdapterDemo adapterDemo) {

this.adapterDemo = adapterDemo;

// Handle mouse clicked.

public void mouseClicked(MouseEvent me) {

adapterDemo.showStatus("Mouse clicked");

class MyMouseMotionAdapter extends MouseMotionAdapter {

AdapterDemo adapterDemo;

public MyMouseMotionAdapter(AdapterDemo adapterDemo) {

this.adapterDemo = adapterDemo;

// Handle mouse dragged.

public void mouseDragged(MouseEvent me) {

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 code="MousePressedDemo" width=200 height=100>

</applet>

*/

public class MousePressedDemo extends Applet {

public void init() {

addMouseListener(new MyMouseAdapter(this));

class MyMouseAdapter extends MouseAdapter {

MousePressedDemo mousePressedDemo;

public MyMouseAdapter(MousePressedDemo mousePressedDemo) {

this.mousePressedDemo = mousePressedDemo;

public void mousePressed(MouseEvent me) {

mousePressedDemo.showStatus("Mouse Pressed.");

Eg. With inner class // Inner class demo.

import java.applet.*;

import java.awt.event.*;

/*

<applet code="InnerClassDemo" width=200 height=100>

</applet>

*/

public class InnerClassDemo extends Applet {

public void init() {

addMouseListener(new MyMouseAdapter());

class MyMouseAdapter extends MouseAdapter {

public void mousePressed(MouseEvent me) {

showStatus("Mouse Pressed");

You might also like