Creating Interfaces-Java Network Programming-Lecture Slides, Slides of Java Programming

This lecture is delivered by Prem Vikas at Jaypee Institute of Information Technology University for discussing following points of Java Network Programming: Interfaces, Event, Handling, Model, Listener, Techniques, Declaration, Constructor, Runnable

Typology: Slides

2011/2012

Uploaded on 07/03/2012

aapti
aapti 🇮🇳

4.6

(28)

80 documents

1 / 29

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Java-Programming
Day-06
Docsity.com
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c
pf1d

Partial preview of the text

Download Creating Interfaces-Java Network Programming-Lecture Slides and more Slides Java Programming in PDF only on Docsity!

Java-Programming

Day-

Outline

• Interfaces

 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

• Anonymous classes

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

  • Declaration

 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- - }

  • Body of interface

 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

  1. public class BasicColorReader {
  2. public class BasicColorReader(){ }
  3. public void BasicColorReader(BasicColor obj){
  4. String colorInformation=obj.getColorInformation();
  5. System.out.println(“Color information = “+colorInformation);
  6. } }

 An object of interface mean object of a class that implements that

constructor

Some interfaces provided by Java

  • AudioClip

 Package: java.applet

 Methods

 Play()  Stop()  Loop()

 Usage

 Implemented by classes for providing playback functionality

  • LayoutManager

 Package : java.awt

 Usage

 Implemented by the classes that want to become a layout manager

  • Runnable

 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 Events

 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..

  • Techniques

 Using interfaces directly

 Using Adapter classess

  • A Generic Example

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

• When a button is pressed

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