8
JAVA Adapter class Prepared by Miss. Arati A. Gadgil

Java adapter

Embed Size (px)

Citation preview

Page 1: Java adapter

JAVA Adapter class

Prepared by

Miss. Arati A. Gadgil

Page 2: Java adapter

2

Adapter class

It is one which contains null body definition for those methods which are inheriting from appropriate Listener.

In java.awt.event.* we have Listener interface called WidowListener which contains seven abstract methods. In the derived class implements WindowListener interface; it is mandatory for derived class to define all the methods even though the derived class is not required.

If the derived class wants to define the required methods, it has to extend its corresponding adapter class called java.awt.event.WindowAdapter and this class contains null body definition for WindowListener interface methods.

Therefore which our Listener interface contains more than one undefined method for thatListener interfaces we have the appropriate adapter class whose general notation is XXXAdapter.

Page 3: Java adapter

public interface WindowListener{

void windowActivated(WindowEvent e);void windowClosed(WindowEvent e);void windowClosing(WindowEvent e);void windowDeactivated(WindowEvent e);void windowDeiconified(WindowEvent e);void windowIconified(WindowEvent e);void windowOpened(WindowEvent e);

}When implementing interface compulsory override all methods.

3

Page 4: Java adapter

Adapters are abstract classes for receiving various events. The methods in these classes are empty. These classes exists as convenience for creating listener objects.

4

Page 5: Java adapter

import java.awt.*; import java.awt.event.*;class frm12 extends Frame implements WindowListener{ frm12()

{ super("window Listener");addWindowListener(this);setSize(300,300);setVisible(true); }

public void windowActivated(WindowEvent e) { }public void windowDeactivated(WindowEvent e) { }public void windowIconified(WindowEvent e) { }public void windowDeiconified(WindowEvent e) { }public void windowOpened(WindowEvent e) { }public void windowClosed(WindowEvent e) { }public void windowClosing(WindowEvent e){System.exit(0);}}public static void main(String []a){ frm12 k=new frm12(); }

} 5

Page 6: Java adapter

import java.awt.*;import java.awt.event.*;

class wadptor extends WindowAdapter{

public void windowClosing(WindowEvent e){

System.exit(0);}

}

WindowListener wl=new wadptor();

Frame.addWindowListener(listener);

6

Page 7: Java adapter

frame.addWindowListener(new wadptor());

frame.addWindowListener(newWindowAdapter(){

public void windowClosing(WindowEvent e){

System.exit(0);}

});

7

Page 8: Java adapter

Thank You

8