Events in java
Download
1 / 11

events in java - PowerPoint PPT Presentation


  • 211 Views
  • Updated On :

Events in Java. CSE 470 - Software Engineering Fall 1999 Updated by J. Brown. Example 1-- Simple Action Event. import java.awt.*; Import java.awt.event.*; import java.applet.*; public class SimpleEvent extends Applet implements ActionListener {

loader
I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.
capcha
Download Presentation

PowerPoint Slideshow about 'events in java' - Ava


An Image/Link below is provided (as is) to download presentation

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript
Events in java l.jpg

Events in Java

CSE 470 - Software Engineering

Fall 1999

Updated by J. Brown


Example 1 simple action event l.jpg
Example 1-- Simple Action Event

import java.awt.*;

Import java.awt.event.*;

import java.applet.*;

public class SimpleEvent extends Applet

implements ActionListener {

Button button1 = new Button("Press me");

public void init() {

this.add(button1);

button1.addActionListener(this);

}

public void actionPerformed(ActionEvent e) {

if(e.getSource() == button1) {

button1.setLabel("Again");

}

}

}


Handling events l.jpg
Handling Events

  • Events are first-class objects.

    • There is a class Event.

    • Subclasses identify different kinds of events.

  • Components fire events.

  • Other objects can listen for/act upon these events.

  • Interested objects register themselves as a Listener with the component that fires the event.


Event routing l.jpg
Event Routing

  • Lots of different kinds of events

    • Different events carry different types of information

    • E.g., ActionEvent carries a command, TextEvent carries a string being edited.

  • Components fire these events.

    • E.g., Button fires an ActionEvent.

    • E.g., TextArea fires a TextEvent.

    • All components fire WindowEvents.

  • When fired, events are routed to special Listener objects.


The awtevent hierarchy l.jpg

AWTEvent

ActionEvent

ComponentEvent

TextEvent

InputEvent

WindowEvent

MouseEvent

KeyEvent

The AWTEvent Hierarchy


Listeners l.jpg
Listeners

  • A listener is an object to which AWTEvents can be routed by components.

  • Different types of listeners.

    • One for each sub-class of AWTEvent.

    • E.g., ActionListener, ComponentListener, WindowListener.

  • A listener class is abstract:

    • Operations only; no attributes or methods.

    • Declared as an interface in Java.


Example an actionevent listener l.jpg
Example: An ActionEvent Listener

import java.awt.*; import java.applet.*;

import java.awt.event.*;

class ButtonListener implements ActionListener {

public void actionPerformed(ActionEvent e) {

System.out.println(“Button pressed!!”);

}

}

public class ActionExample extends Applet {

public void init() {

Button button = new Button(“Push me”);

button.addActionListener(new ButtonListener());

button.addMouseListener(new ButtonMouseListener());

add(button);

}

}


Example a mouseevent listener l.jpg
Example: A MouseEvent Listener

class ButtonMouseListener implements MouseListener {

public void mouseEntered(MouseEvent e) {

System.out.println(“Mouse entered button!”);

};

public void mouseExited(MouseEvent e) {

System.out.println(“Mouse exited button!”);

};

public void mousePressed(MouseEvent e) {}

public void mouseClicked(MouseEvent e) {}

public void mouseReleased(MouseEvent e) {}

}


Mouse events l.jpg
Mouse Events

  • mouseClicked(MouseEvent evt);

    • Called when the mouse button is clicked

  • mousePressed(MouseEvent evt);

    • Called when the mouse button is depressed

  • mouseReleased(MouseEvent evt);

    • Called when the mouse button is released


More mouse events l.jpg
More Mouse Events

  • mouseEntered(MouseEvent evt);

    • Called when the mouse enters the component

  • mouseExited(MouseEvent evt);

    • Called when the mouse leaves the component

  • mouseMoved(MouseEvent evt);

    • Called when the mouse moves with no mouse button depressed

  • mouseDragged(MouseEvent evt);

    • Called when the mouse moves with the mouse button depressed


References l.jpg
References

  • G. Cornell and C. Horstmann, “Core Java”, The Sunsoft Press Java Series, Second Edition, 1997

  • D. Joshi, L. Lemay, and C. Perkins, “Teach yourself Java in Café in 21 days”, Sams.net publishing, 1996

  • The Java Tutorial

    • http://java.sun.com/docs/books/tutorial/index.htm

  • D. Geary and A. McClellan, “Graphic Java”, The Sunsoft Press Java Series, 1997


ad