Solved

Receiving Events for custom events

Posted on 1998-08-19
4
199 Views
Last Modified: 2008-02-01
Can anyone tell me why the
TestSourceComponent.processEvent() method isn't being called?  And/or what happens (has to happen) when you throw and event on the system event queue in order for me to catch the event.  Or is there a better way of throwing an event so that I can catch it?  It does work if I explicitly call it through the TestMulticaster class.  (This is just for my own test, I realize that the Multicaster is not implemented as it should.  I'm more interested in passing and listening for the message)

here is what I have done

// TestEventListener.java
public interface TestEventListener extends EventListener
{
     public void EventThrown(TestEvent evt);
}

*******************************************************
//TestEvent.java
public class TestEvent extends AWTEvent
{
      public final static int TESTEVENTID = 2000;
        Object _source = null;
        String _Message = null;

     public TestEvent(Object source, String mess)
     {
         super(source, TestEvent.TESTEVENTID);
         _source = source;
         _Message = mess;
     }
     public String getMessage()
     {
         return _Message;
     }
 
     public Object getSource()
     {
           return _source;
     }
}

*********************************************************

//TestMulticaster.java
     private static Vector v = new Vector();
     private static TestMulticaster _Multi = null;

     private TestMulticaster()
     {
     }

     public static TestMulticaster instance()
     {
           if( _Multi == null )
              _Multi = new TestMulticaster();
           return _Multi;
     }
     public static void add(TestEventListener l)
     {
          v.addElement(l);
     }

     public static void remove(TestEventListener l)
     {
          v.removeElement(l);
     }

     public void EventThrown(TestEvent e)
     {
      System.out.println("EventThrown of TestMulticaster.java");
        System.out.println("throwing event to all of the event listeners that were registered");
      Enumeration enum = v.elements();
         while(enum.hasMoreElements())
         {
              ((TestEventListener)enum.nextElement()).EventThrown(e);
         }
     }    
}

*********************************************************
/TestSourceComponent.java
public class TestSourceComponent
{
    public void addTestEventListener(TestEventListener tel)
    {
         TestMulticaster.instance().add(tel);
    }

    public void removeTestEventListener(TestEventListener tel)
    {
         TestMulticaster.instance().remove(tel);
    }

    boolean eventEnabled(AWTEvent e)
    {
        System.out.println("in eventEnabled");
        if (e.getID() == TestEvent.TESTEVENTID)
        {
           System.out.println("Got TestEvent!!!");
           return true;
        }
        return false;
    }      

    protected void processEvent(AWTEvent e)
    {
        System.out.println("in processEvent");
        if (e instanceof TestEvent) {
            System.out.println("instanceof TestEvent! " + e);
            processTestEvent((TestEvent)e);    
            return;
        }
      else
            System.out.println("Not an instanceof TestEvent! " + e);
      //super.processEvent(e);
    }

    protected void processTestEvent(TestEvent e) {
        //if (actionListener != null) {
        //    actionListener.actionPerformed(e);
        //}
         System.out.println("in processTestEvent");
         TestMulticaster.instance().EventThrown(e);
    }
   
}

*********************************************************
// INIManager.java
public class INIManager
{    
    public void testIni(Object o)
    {
        Toolkit.getDefaultToolkit().getSystemEventQueue().postEvent(new TestEvent(o, "addTrace:In INIManager testIni"));
        Toolkit.getDefaultToolkit().getSystemEventQueue().postEvent(new TestEvent(o, "statusRequested:In INIManager  testIni"));
    }
}

**********************************************************
//TestSource.java
public class TestSource
{

    public static void main(String args[])
    {
      TestSourceComponent t = new TestSourceComponent();
        SymListener SymListenerVar = new SymListener();
        //enable events????
        t.addTestEventListener( SymListenerVar );
        t.addTestEventListener( SymListenerVar );
        INIManager thrower = new INIManager();
        thrower.testIni(t);
/*        TestMulticaster.instance().EventThrown(new TestEvent(t, "testing this") );
        t.removeTestEventListener( SymListenerVar );
        TestMulticaster.instance().EventThrown(new TestEvent(t, "second test") );  
*/
    }
}
0
Comment
Question by:smithcb
  • 2
  • 2
4 Comments
 
LVL 3

Accepted Solution

by:
mjenkins earned 100 total points
ID: 1222107
Here is a simple but complete example of how to create and generate a custom event.


import java.awt.*;
import java.awt.event.*;
 
public class TComponent
        extends Component
{
        public static void main( String args[] )
        {
                Frame f = new Frame("TComponent");
                f.setSize(100,100);
                TComponent t = new TComponent();
                f.add(t);
                f.setVisible(true);
                Toolkit.getDefaultToolkit().getSystemEventQueue().postEvent( new MyEvent(t,8192) );
        }
 
        public TComponent()
        {
                enableEvents( 8192 );
        }
 
        public void processEvent( AWTEvent e )
        {
                System.out.println( "GOT IT" );
                super.processEvent(e);
        }
}
 
class MyEvent
        extends AWTEvent
{
        public MyEvent( Object src, int id )
        {
                super( src, id );
        }
}

0
 

Author Comment

by:smithcb
ID: 1222108
Does this mean that one cannot create an event and catch that event without extending Component?  I thought that jdk1.1.6 had means to do this without extending Component...
0
 
LVL 3

Expert Comment

by:mjenkins
ID: 1222109
If you want to use the system event queue, yes. YOu should subclass component. You can always create your own event queue and dispatch events that way.
0
 

Author Comment

by:smithcb
ID: 1222110
BTW, sorry it took so long for me to get back to you... (re: your first answer)  I had posted a similar comment about 3 weeks ago but I guess it didn't take.  I figured I should find out what was taking so long and noticed it wasn't there.  Thanks for your help.

0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
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…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
This video teaches viewers about errors in exception handling.

726 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