Solved

Receiving Events for custom events

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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Suggested Solutions

For customizing the look of your lightweight component and making it look lucid like it was made of glass. Or: how to make your component more Apple-ish ;) This tip assumes your component to be of rectangular shape and completely opaque. (COD…
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 …
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
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:

816 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

11 Experts available now in Live!

Get 1:1 Help Now