Solved

Receiving Events for custom events

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
GUI builder for Eclipse? 8 27
Java Timer (static) 9 36
Java SE 8u111  Lot of stuff broke 11 54
Arrays.asList  VS  ArrayList 4 45
An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
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…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.

910 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

22 Experts available now in Live!

Get 1:1 Help Now