Solved

User Defined Event

Posted on 1998-07-08
4
235 Views
Last Modified: 2010-03-30
Hello!

Is there "user defined event" in Java? Can I define a event?
If there is and I can, how can I add it to EventListener?
I want to make threads communicate via user defined events and EventListener.

What I want to do is under.
One process and a few threads.
When threads get some data from outside they make "main process" know it
and pass the data to "main process".
"main process" processes the data and sends back to the threads.

Let me know it and I need sample source code.
Thank you.
0
Comment
Question by:lmh99
  • 2
  • 2
4 Comments
 

Author Comment

by:lmh99
ID: 1225363
I'm novice in Java.
So I need detailed sample codes with comments.
0
 
LVL 5

Accepted Solution

by:
msmolyak earned 100 total points
ID: 1225364
Several steps.

1. Create a class corresponding to the new event type as following.

import java.util.EventObject;

public class MyEvent extends EventObject
{
    public MyEvent(Object eventSource)
    {
        super(eventSource);
    }
   
    // You can override getSOurce() and toString() methods and
    // add any methods you want. You can add more constructors if
    // you wish to pass some extra information.
}

2. Create an event listener interface for this event type.

public interface MyEventListener implements EventListener
{
    // This method should be called when the new event takes place.
    // Its name should reflect the nature of the event (focusGained(), actionPerformed())
    public void somethingHappened(MyEvent e);
}

3. In the class where you plan to generate the new event you should implement the following two methods. In those methods you will maintain some structure (e.g., a Vector) containing the list of registered listeners.  You can look at the implementation of AWTEventMulticaster class which is a sophisticated nested structure which is used to add and remove registered listeners in AWT classes. The implementation of addXXXListener uses that class to add listeners (same with remove), the same class is used to notify all the listeners when the event takes place.  

public void addMyEventListener(MyEventListener listener)
{
}

public void removeMyEventListener(MyEventListener listener)
{
}

4. When event takes place, create a class of type MyEvent and notify all the registered listeners about it by calling listener's somethingHappened() method and passing the newly constructed event to it.  You can implement a method

public void processEvent(MyEvent e)
{
}

In this method you iterate through all the registered listeners and notify them about the event. If you choose to go with the Vector for storing listeners, it is a good idea to clone the vector before notifying the listeners and do it in the synchronized block. If you choose to extend the AWTEventMulticaster, this should be taken care of.

Hope that helps.

Michael
0
 

Author Comment

by:lmh99
ID: 1225365
Thanks again, Michael.
Please give me the whole code.
I want to execute and analyze it by myself.
0
 
LVL 5

Expert Comment

by:msmolyak
ID: 1225366
Unfortunately I do not have a complete example (I am sure they exist somewhere). I was writing the code on the fly.

Let me give you an example implementation:

Vector listeners;

public void addMyEventListener(MyEventListener listener)
{
  listeners.addElement(listener);
}

public void removeMyEventListener(MyEventListener listener)
{
  listeners.removeElement(listener);
}

public void processEvent(MyEvent e)
{
  // Notify all the listeners about the event
  synchronized(listeners)
  {
    Vector clone = (Vector) listeners.clone();
    Enumeration enum = clone.elements();
    while(enum.hasMoreElements())
    {
      MyEventListener listener = (MyEventListener)enum.nextElement();
      listener.somethingHappened(e);
    }
  }
}

// The last step is to create the new event and process it.
// Somewhere in your code where the event happens
.
MyEvent event = new MyEvent(...);
processEvent(event);
.

Hope that helps.
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
windows explorer path to command prompt 5 54
how to add new optional parameter to JSP 1 49
java stored proc example 9 46
Tagging and Merging on Branch 1 30
Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

830 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question