Upload
arati-gadgil
View
157
Download
0
Embed Size (px)
Citation preview
JAVA Adapter class
Prepared by
Miss. Arati A. Gadgil
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.
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
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
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
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
frame.addWindowListener(new wadptor());
frame.addWindowListener(newWindowAdapter(){
public void windowClosing(WindowEvent e){
System.exit(0);}
});
7
Thank You
8