Solved

Receiving Events for custom events

Posted on 1998-08-19
4
194 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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

For customizing the look of your lightweight component and making it look opaque like it was made of plastic.  This tip assumes your component to be of rectangular shape and completely opaque.   (CODE)
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
This video teaches viewers about errors in exception handling.

760 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

19 Experts available now in Live!

Get 1:1 Help Now