Solved

User Defined Event

Posted on 1998-07-08
4
242 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Enroll in May's Course of the Month

May’s Course of the Month is now available! Experts Exchange’s Premium Members and Team Accounts have access to a complimentary course each month as part of their membership—an extra way to increase training and boost professional development.

Question has a verified solution.

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

Suggested Solutions

Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
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…
The viewer will learn how to implement Singleton Design Pattern in Java.
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.
Suggested Courses

751 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