Download Java Interfaces and Event Handling and more Slides Java Programming in PDF only on Docsity!
ava - programming
An Introduction
Java Short Course
Day-
J
Outline
What? Why?
Creating Interfaces
Implementing interfaces
- Few Interfaces
- Event Handling
Java’s Event Model
Event Listener
Event Handling Techniques
java.awt.event package
Keyboad and Mouse Events
Interfaces..cont..
One of the key features in software development
Various programs communicating with each other require standard
set of methods to be present
A software saying I have printAble interface implemented mean it
an communicate with printer
Example
EditAble interface
Methods
Cut Copy Paste
Constant
Clipboard capacity
Creating Interfaces
An interface is simply declared using keyword “interface”
interface selfService{ -- - - - body of interface---}
Optionals
public private protected Extends interface EditAble { - - - - -body of interface - - - - - } public interface EditAble{ - - - - body of interface - - - } public interface EditAbleGraphics extends EditAble{- - - -body- - }
Methods with no implementation
Methods are public by default public void cut(); public void paste(); public void copy(); Version- public void cut(int start,int end);
Constants
Constants are static and final by default Must be initialized when declared int LIMIT=2;
Interface-Example
The BasicColors interface
public interface BasicColors{
String RED=“Red”;
int GREEN=“Green”;
int BLUE=“Blue” ;
public void changeToRed(); //method with no body
pubic void changeToGreen();
public void changeToBlue();
Save this interface to a file BasicColors.java
Compile it [you will get BasicColor.class]
Using an interface
- Using an interface means implementing in a class
A class must declare in its definition the name of interface it is implementing
using implements keyword
Example
public class TrafficSignal implements BasicColors{ -----body------- }
After declaration the class must have to provide the implementation of the all the
method in an interface
Example
package InterfaceTest;
public class TrfficSignal implements BasicColors{ public void changeToRed(){ System.out.println(“The color of the signal is RED”); } public void changeToGreen(){ System.out.println(“The color of the signal is GREEN”); } public void changeToBlue(){ System.out.println(“The color of the signal is BLUE”); } }
Using interfaces as abstract type
- Interfaces are good for using as abstract data type
- Often uses as parameter to a constructor or a method
Example
- public class BasicColorReader {
- public class BasicColorReader(){ }
- public void BasicColorReader(BasicColor obj){
- String colorInformation=obj.getColorInformation();
- System.out.println(“Color information = “+colorInformation);
- } }
An object of interface mean object of a class that implements that
constructor
Some interfaces provided by Java
Package: java.applet
Methods
Play() Stop() Loop()
Usage
Implemented by classes for providing playback functionality
Package : java.awt
Usage
Implemented by the classes that want to become a layout manager
Package: java.lang
Methods
run()
Usage
Implemented by any class whose instances are intended to be executed by a thread
Event Handling
- Events! Where do they come from
When a change in a program occur an even it generted
When ever a change occurs appropriate even object is created and passed either
to operating system or to some other event handler
Java intercepts events generated , translate them and route them to proper
event handler
Example
When a button of an applet is pressed A button press event object is generated Applet translates the event and try to find some handler in the applet If a handler in the program is found the event is handed over to it If no handler is found the default handler is used
Handling Event: detecting an Event and doing something useful
A class must listen for some event
To listen an event it must register for listening itself for that event
Registration require implementation of proper event listener interface
Java provide a rich set of interfaces for events
A component must specify its event listener for its events to be processed
event Handling cont..
Using interfaces directly
Using Adapter classess
public class MyApplet extends Applet implements KeyListener{
- - - -code for the class - - - - - -- - - - - - - - - - - -code for ithe mplementation of KyListener methods - - -
Just by looking “implements KeyListener” java comes to know that this
class is listening for keyboard events
A component in some class can choose this class to listen for its events
and handle them
Execution of the code
- Java registers MyApplet for Button Clicks
A component can use this class for listening its event
Whenever a buttonclick event is received by MyApplet it will execute its
actionPerformed() method
- Button b1 assigns its event listening to MyApplet
When ever this button b1 is clicked the event will handed over to
MyApplet by Java
actionPerformed() method in MyApplet executed
complete program
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
public class myApplet extends Applet implements ActionListener{ //class declaration
private Button button1 = new Button();
public void init () {
button1.setText(“Click It");
button1.addActionListener(this); //specifying the action listener
add(button1);
}
//implementing method of ActionListener
public void actionPerformed(ActionEvent event){
System.out.println("The Button is pressed");
} }
Handling events for more than one button
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
public class myApplet extends Applet implements ActionListener{
private Button button1 = new Button();
private Button button2 = new Button();
public void init () {
button1.setText(“Click It");
button1.addActionListener(this);
button2.addActionListener(this);
add(button2);
add(button1); }
//implementing method of ActionListener
public void actionPerformed(ActionEvent event){
System.out.println("The Button is pressed");
} }
Solution-
- A separate listener class for each button or component
Example
Class 1 for button
class button1_listener implements ActionListener{
public void actionPerformed(ActionEvent e){ System.out.println(“Button 1 is pressed “); } }
Class 2 for button
class button2_listener implements ActionListener{
public void actionPerformed(ActionEvent e){ System.out.println(“Button 2 is pressed “); } }