Sunday, October 12, 2008

Abstract, interface,listners -ii

hi....I hope u people bit get idea abt adapters, listners..by reading previous post...
to make easy to understand demonstrated with example..
Simple Event Handler
Start with a simple example. Suppose we want to print "Hello" when a button is pressed. Button fires an actionPerformed event when pressed, which is the only method in the ActionListener interface. We could write the following code to accomplish this task:
SayHello.javaimport java.awt.*;
import java.awt.event.*;
public class SayHello implements ActionListener {
public void actionPerformed(ActionEvent e) {
System.out.println("Hello");
}
}

EX.java
import java.awt.*;
public class EX{
public static void main(String[] args) {
Frame f = new Frame();
Button b = new Button("Press me");
b.addActionListener(new SayHello());
f.add(b);
f.setVisible(true);
// application exit handler omitted
}
}
Simple Anonymous Inner Class
It seems like a waste to create an entirely new class just to say "Hello". This uses up a class name in our namespace, and unique names can be difficult to create.
To help this, we create an anonymous inner class by writing the event handler class code in the call to register the event handler. The new test class looks as follows:
ex2.javaimport java.awt.*;
import java.awt.event.*;
public ex2{
public static void main(String[] args) {
Frame f = new Frame();
Button b = new Button("Press me");
b.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.out.println("Hello");
}});
f.add(b);
f.setVisible(true);
// application exit handler omitted
}
}
ActionListener is an interface! Whenever you see "new" followed by an interface name and a "{" (open curly brace), you're creating an anonymous inner class. You would readnew ActionListener() { ... }
as "Create a new instance of some class (we don't care what its name is) that implements ActionListener, and the details are in the curly braces."
This is a convenient shorthand for creating event handlers.
Event Adapters
Some event listener interfaces require several methods. For example, WindowListener requires seven methods. Normally, you only care about one of these methods, windowClosing. If we were to write an anonymous inner class to close an application, it might look as followsFrame f = new Frame();
f.addWindowListener(new WindowListener() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
public void windowClosed(WindowEvent e) {}
public void windowOpened(WindowEvent e) {}
public void windowIconified(WindowEvent e) {}
public void windowDeiconified(WindowEvent e) {}
public void windowActivated(WindowEvent e) {}
public void windowDeactivated(WindowEvent e) {}
});
All of those dummy methods are really a lot of noise. In java.awt.event, Sun defined a class called WindowAdapter that implements WindowListener with seven dummy methods. This means that you can extend WindowAdapter and you only need to override the classes you care about. The above code becomes:Frame f = new Frame();
f.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}});
You read the anonymous inner class above as "create an instance of some class (we don't care what its name is) that extends WindowAdapter, and the details are in the curly braces."
The full example for the button press and window close follows:
SimpleGUI3.javaimport java.awt.*;
import java.awt.event.*;
public class EXGUI3 {
public static void main(String[] args) {
Frame f = new Frame();
Button b = new Button("Press me");
b.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.out.println("Hello");
}});
f.add(b);
f.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}});
f.setVisible(true);
// application exit handler omitted
}
}

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

Wednesday, June 11, 2008

Java new Innovation....

here is interview about new innovation of java
i have watched interview it really good Mr.Bob as explained neatly and java fx and its uses and the future plans....better u watch..and pls dont forget to leave the comments
thank you....

Host: Kuldip Oberoi
Guest: Bob Brewin

http://blogs.sun.com/SDNChannel/entry/the_latest_java_innovation#comment-1182083138000