Lecture8 oopj
Upcoming SlideShare
Loading in...5
×
 

Like this? Share it with your network

Share

Lecture8 oopj

on

  • 302 views

 

Statistics

Views

Total Views
302
Views on SlideShare
302
Embed Views
0

Actions

Likes
0
Downloads
1
Comments
0

0 Embeds 0

No embeds

Accessibility

Categories

Upload Details

Uploaded via as Microsoft Word

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment

Lecture8 oopj Document Transcript

  • 1. Java Event HandlingIn AWT components, we came to know everycomponent (except Panel and Label) generatesevents when interacted by the user like clicking overa button or pressing enter key etc. Listeners handlethe events. Let us know the style (or design pattern)Java follows to handle the events.The event handling involves three types of classes. 1. Event Sources 2. Event classes 3. Event Listeners1. Event SourcesEvent sources are components, subclasses ofjava.awt.Component, capable to generate events.The event source can be a button, TextField or aFrame etc.java.awt.ComponentAfter having the basic idea of Java AWT, let us knowhow many component classes exist with java.awtpackage.
  • 2. 2. Event classesAlmost every event source generates an event and isnamed by some Java class. For example, the buttongenerates ActionEvent and Checkbox generatesItemEvent. All events listed in java.awt.eventpackage. Following list gives a few components andtheir listeners.
  • 3. 3. Event ListenersThe events generated by the GUI components arehandled by a special group of classes known as"listeners". Listener is an interface. Everycomponent has its own listener, say,AdjustmentListener handles the events of scrollbarSome listeners handle the events of a fewcomponents. For example, ActionListener handlesthe events of Button, TextField, List and Menus.Listeners are from java.awt.event package.
  • 4. Handle Action Events for AWT Button Exampleimport java.applet.Applet;import java.awt.Button;import java.awt.Graphics;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;/*
  • 5. <applet code="HandleActionEventExample"width=200 height=200></applet>*/public class HandleActionEventExample extendsApplet implements ActionListener{ String actionMessage=""; public void init() { //create Buttons Button Button1 = new Button("Ok"); Button Button2 = new Button("Cancel"); //add Buttons add(Button1); add(Button2); //set action listeners for buttons Button1.addActionListener(this); Button2.addActionListener(this); } public void paint(Graphics g) { g.drawString(actionMessage,10,50);
  • 6. }public void actionPerformed(ActionEvent ae) { String action = ae.getActionCommand(); if(action.equals("Ok")) actionMessage = "Ok Button Pressed"; else if(action.equals("Cancel")) actionMessage = "Cancel ButtonPressed"; repaint(); } }