On Thu, 07 Aug 2003 22:39:01 GMT, Hal Vaughan <ha*@thresholddigital.com>
two-finger typed:
I've said here before that I'm self taught. I have 4 reference books, and
they mention listeners and give partial examples, but not one gives me a
good, clear example of setting up a listener, using it, and disposing it.
Can anyone either point me to some good examples online, or show me one?It
would save me a day or two of try different ways to do it.
Thanks for any help.
Hal
A listener is basically a class that implements the method or methods
described in the listener interface, such as java.awt.event.ActionListener.
I. using a listener
Just add the listener implemenation to an 'event' source:
myButton.addActionListener(myListener);
II. disposing of a listener
Just remove the listener implemenation from the 'event' source:
myButton.removeActionListener(myListener);
III. creating a listener
You can do this in three different ways:
1) make one of your public classes implement the listener itself.
2) create an anonymous inner class that creates the listener.
3) create a named inner class that implements the listener.
1: Example1Class.java
import java.awt.* ;
import java.awt.event.* ;
// Advantages:
// - no complex code
// - external classes can issue the command
// (accessibility e.g. by using Example1Class.COMMAND)
// - listener can be removed again (disable method in this case).
// - single class file implementation.
//
// Disadvantage:
// - events can be spoofed (made look like they are authentic
// and come from myButton without user interaction).
public Example1Class implements ActionListener {
public final static String COMMAND = "CLICKED";
Button myButton = new Button("Click here");
public Example1Class(Container parent) {
super();
myButton.setActionCommand(COMMAND);
myButton.setBounds(10,10,200,20);
parent.add(myButton);
}
public void enable() {
myButton.addActionListener(this);
}
public void disable() {
myButton.removeActionListener(this);
}
public void actionPerformed(ActionEvent event) {
Object source = event.getSource();
String action = event.getActionCommand();
System.out.println(
"Action issued from "+source.toString());
System.out.println("Command: "+action);
}
}
2: Example2Class.java
import java.awt.* ;
import java.awt.event.* ;
// Advantages:
// - no complex code
// - information hiding, security (no spoofing):
// only the button can/will issue the events.
//
// Disadvantages:
// - listener cannot be removed easily, since no reference exists
// (except in the button itself).
// - two class files to distribute (one with added $1 in the name)
public Example2Class {
Button myButton = new Button("Click here");
public Example2Class(Container parent) {
super();
myButton.setActionCommand("CLICKED");
myButton.setBounds(10,10,200,20);
parent.add(myButton);
}
public void enable() {
myButton.addActionListener(new ActionListener(
{
public void actionPerformed(ActionEvent event) {
Object source = event.getSource();
String action = event.getActionCommand();
System.out.println(
"Action issued from "+source.toString());
System.out.println("Command: "+action);
}
});
}
}
3: Example3Class.java
import java.awt.* ;
import java.awt.event.* ;
// Advantages:
// - information hiding, security (no spoofing):
// only the button can/will issue the events.
// - listener can be removed again (disable method in this case).
//
// Disadvantages:
// - more complex code
// - two class files to distribute (one with added $Respond)
public Example3Class {
Button myButton = new Button("Click here");
public Example2Class(Container parent) {
super();
myButton.setActionCommand("CLICKED");
myButton.setBounds(10,10,200,20);
parent.add(myButton);
}
class Respond {
public void actionPerformed(ActionEvent event) {
Object source = event.getSource();
String action = event.getActionCommand();
System.out.println(
"Action issued from "+source.toString());
System.out.println("Command: "+action);
}
} responder = new Respond();
public void enable() {
myButton.addActionListener(responder);
}
public void disable() {
myButton.removeActionListener(responder);
}
}
Have fun,
Cheers.