Sunday, October 12, 2008

Adapter, Interface and listeners in java

Adapter classes:
Many listener interfaces have more than one callback method.
Example:java.awt.FocusListener that has two methods;
focusGained(java.awt.FocusEvent event) and
focusLost(java.awt.FocusEvent event).
When creating a listener class that implements the interface the Java compiler insists that all of the interface methods are implemented, which often results in many empty methods being created to satisfy its requirements when only one or some of its methods actually contain code. The following statement shows a FocusListener being used to perform some logic when a Java bean gains focus.
However, an empty focusLost method must be provided.
Component.addFocusListener(new java.awt.event.FocusListener() {
public void focusGained(java.awt.event.FocusEvent e) {
doFocusGainedCode();
}
public void focusLost(java.awt.event.FocusEvent e) {
}
});

To avoid having many empty listener methods for many listeners,
Adapter classes are provided. These implement the listener interface, and provide empty no-op implementation of its methods. The advantage is that the listener can extend these, and only specialize methods of choice without having to provide default implementations for the rest ( these are inherited from the Adapter ).
Component.addFocusListener(new java.awt.event.FocusAdapter() {
public void focusGained(java.awt.event.FocusEvent e) {
doFocusGainedCode();
}
});

for making easy to under stand i will continue this with example..in next post...Thank u..comments welcome....?

2 comments:

Anonymous said...

namge idhru bage.. gothilla.. commment kodoNa andhre.. sisya.. mundhu varisu..

ha ha.. said...

maga.. yaak nillisbitte.. innu maathaadu..