Easy to Learn Java: Programming Articles, Examples and Tips

Start with Java in a few days with Java Lessons or Lectures


Code Examples

Java Tools

More Java Tools!

Java Forum

All Java Tips


Submit News
Search the site here...
Search the JavaFAQ.nu
1000 Java Tips ebook

1000 Java Tips - Click here for the high resolution copy!1000 Java Tips - Click here for the high resolution copy!

Java Screensaver, take it here

Free "1000 Java Tips" eBook is here! It is huge collection of big and small Java programming articles and tips. Please take your copy here.

Take your copy of free "Java Technology Screensaver"!.

Lecture 18: Writing event listeners with best examples, Part I

JavaFAQ Home » Java Lectures by Anatoliy Malyarenko Go to all tips in Java Lectures by Anatoliy Malyarenko

Bookmark and Share

Writing event listeners

by: Anatoliy Malyarenko


Contents of the lecture.

  • Introduction to event listeners.
  • General information about writing event listeners.
  • Implementing listeners for commonly handling events.

Introduction to event listeners

Let's look at one of the simplest event handling examples possible. It's called Beeper, and it features a button that beeps when you click it.

Here's the code that implements the event handling for the button:


public class Beeper ... implements ActionListener {
    //where initialisation occurs:
    public void actionPerformed(ActionEvent e) {
       ...//Make a beep sound...

The Beeper class implements the ActionListener interface, which contains one method: actionPerformed. Since Beeper implements ActionListener, a Beeper object can register as a listener for the action events that buttons fire. Once the Beeper has been registered using the addActionListener method, the Beeper's actionPerformed method is called every time the button is clicked.

The event model, which you saw at its simplest in the preceding example, is quite powerful and flexible. Any number of event listener objects can listen for all kinds of events from any number of event source objects. For example, a program might create one listener per event source. Or a program might have a single listener for all events from all sources. A program can even have more than one listener for a single kind of event from a single event source.

Each event is represented by an object that gives information about the event and identifies the event source. Event sources are often components or models, but other kinds of objects can also be event sources.

The following example demonstrates that event listeners can be registered on multiple objects and that the same event can be sent to multiple listeners. The example contains two event sources (JButton instances) and two event listeners. One of the event listeners (an instance of a class called MultiListener) listens for events from both buttons. When it receives an event, it adds the event's "action command" (which is set to the text on the button's label) to the top text area. The second event listener (an instance of a class called Eavesdropper) listens for events on only one of the buttons. When it receives an event, it adds the action command to the bottom text area.

Here's the code that implements the event handling for the button:


    public class MultiListener ... implements ActionListener {

        //where initialization occurs:
        button2.addActionListener(new Eavesdropper(bottomTextArea));

    public void actionPerformed(ActionEvent e) {
        topTextArea.append(e.getActionCommand() + newline);

class Eavesdropper implements ActionListener {
    public void actionPerformed(ActionEvent e) {
        myTextArea.append(e.getActionCommand() + newline);

In the above code, both MultiListener and Eavesdropper implement the ActionListener interface and register as action listeners using the JButton addActionListener method. Both classes' implementations of the actionPerformed method are similar: they simply add the event's action command to a text area.

to be continued

 Printer Friendly Page  Printer Friendly Page
 Send to a Friend  Send to a Friend

.. Bookmark and Share

Search here again if you need more info!
Custom Search

Home Code Examples Java Forum All Java Tips Books Submit News, Code... Search... Offshore Software Tech Doodling

RSS feed Java FAQ RSS feed Java FAQ News     

    RSS feed Java Forums RSS feed Java Forums

All logos and trademarks in this site are property of their respective owner. The comments are property of their posters, all the rest 1999-2006 by Java FAQs Daily Tips.

Interactive software released under GNU GPL, Code Credits, Privacy Policy