Solved

User Defined Event

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

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

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…
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 one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
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.

708 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

21 Experts available now in Live!

Get 1:1 Help Now