Solved

User Defined Event

Posted on 1998-07-08
4
219 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
This video teaches viewers about errors in exception handling.

896 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now