473,398 Members | 2,188 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,398 software developers and data experts.

Would Like to See Examples of Listeners

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
Jul 17 '05 #1
4 2991
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.
Jul 17 '05 #2
Hal Vaughan <ha*@thresholddigital.com> wrote in message news:<8yAYa.60341$cF.20558@rwcrnsc53>...
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.
What do you mean by 'partial' ?

You're unlikely to find a tutorial or examples which focus specifically
on listeners, as they are merely the mechanics which enable events to be
delivered from one part of a Java application to another, and as such
they tend to be taught as part of something bigger (usually AWT/Swing!)
rather than as an isolated topic in their own right.

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.


There's nothing magically about listeners. A given piece of Java code
which may be required to signal events to other parts of an application
(for example, a button when it is clicked) will provide the facility to
remember one or more listeners, who are interested in receiving notifi-
cation of its events. The interested parties are responsible for
registering themselves with the event source, so the source knows of
their existance and can dispatch events to them, as and when...

The actual coupling between event source and listener is typically done
in the form of an interface, which each interested party implements, to
provide methods which the event source can then call to signal that a
given event has occured - typically passing over an event object which
contains specific details of the event.

There are certain naming patterns (conventions) which apply. They are
not mandatory, but they do make the code more readable. Each set of
events is known by a given name, and that name is then used as the
basis of the various classes, interfaces and methods which define the
listener mechanism for that set of events.

As some listeners handle several events (several event methods) you'll
also find 'adapter' classes are often available to help minimise the
amount of coding needed to implement a listener. Adapters provide a
default implementation of an interface (typically not doing anything
useful with the events) allowing the programmer to override only those
event handling methods she is interested in - rather than providing
code for *every* method in the listener interface. You'll usually find
that adapters are build using inner classes of the class which is actua-
lly interested in the event - but they don't *have* to be!
-FISH- ><>
Jul 17 '05 #3
Wow!

I've been wading through my books, the web, the OpenOffice.org idl ref and
the Java SDK references. You did a great job at simplifying everything and
making it quite clear. I didn't realize it was so simple.

Hal

Neomorph wrote:
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.


Jul 17 '05 #4
FISH wrote:
Hal Vaughan <ha*@thresholddigital.com> wrote in message
news:<8yAYa.60341$cF.20558@rwcrnsc53>...
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.
What do you mean by 'partial' ?

You're unlikely to find a tutorial or examples which focus specifically
on listeners, as they are merely the mechanics which enable events to be
delivered from one part of a Java application to another, and as such
they tend to be taught as part of something bigger (usually AWT/Swing!)
rather than as an isolated topic in their own right.

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.


There's nothing magically about listeners. A given piece of Java code
which may be required to signal events to other parts of an application
(for example, a button when it is clicked) will provide the facility to
remember one or more listeners, who are interested in receiving notifi-
cation of its events. The interested parties are responsible for
registering themselves with the event source, so the source knows of
their existance and can dispatch events to them, as and when...


That's what it took me time to figure out. I found so little on them, it
seemed like there was something going on that I was missing. It took me a
few days of poking around to finally get one working correctly.
The actual coupling between event source and listener is typically done
in the form of an interface, which each interested party implements, to
provide methods which the event source can then call to signal that a
given event has occured - typically passing over an event object which
contains specific details of the event.

There are certain naming patterns (conventions) which apply. They are
not mandatory, but they do make the code more readable. Each set of
events is known by a given name, and that name is then used as the
basis of the various classes, interfaces and methods which define the
listener mechanism for that set of events.

As some listeners handle several events (several event methods) you'll
also find 'adapter' classes are often available to help minimise the
amount of coding needed to implement a listener. Adapters provide a
default implementation of an interface (typically not doing anything
useful with the events) allowing the programmer to override only those
event handling methods she is interested in - rather than providing
code for *every* method in the listener interface. You'll usually find
that adapters are build using inner classes of the class which is actua-
lly interested in the event - but they don't *have* to be!
Actually, I found more on "adapter" classes than on just listeners.
Thanks!

Hal
-FISH- ><>


Jul 17 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: mathon | last post by:
Hello, could anybody explain me the advantages and disadvantages of delegation and the advantages and disadvantages using event-listeners? - both related to the treatment of events. thanks in...
1
by: pokémon | last post by:
Ok, I am baffled by this one: I have 2 custom trace listeners (classes that inherit from System.Diagnostics.TraceListener). I want them both to listen to the same Windows application. How...
1
by: Fred Mellender | last post by:
When I code: Debug.Assert(false, "this is a message"); it works fine, and out comes the dialog box. When I code Trace.Listeners.Clear(); Debug.Assert(false, "this is a message"); No message...
2
by: Michael Powe | last post by:
Hello, Is there a way (in JS) to detect listeners on an element and identify them? The purpose would be to selectively attach additional listeners if certain conditions are met. For example, I...
0
by: msnews.microsoft.com | last post by:
Hi, I am tring to use Trace Listener in asp.net 1.1 application as mentoned here. But i am getting the following error, Configuration Error Description: An error occurred during the processing...
1
by: Armin Zingler | last post by:
Hi, I add a TraceListener to Debug.Listeners. After that I use Debug.writeline. Problem: Nothing arrives at the listener. Code: Dim ts As New TraceStream Debug.Listeners.Add(New...
6
by: Zytan | last post by:
According to http://www.15seconds.com/issue/020910.htm I am doing this in the c'tor of a 'logfile' class: objStream = new System.IO.FileStream(logFilename, System.IO.FileMode.OpenOrCreate);...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.