Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

User Defined Event

Posted on 1998-07-08
4
Medium Priority
?
256 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 300 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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
Suggested Courses

927 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