Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Receiving Events for custom events

Posted on 1998-08-19
4
Medium Priority
?
212 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
4 Comments
 
LVL 3

Accepted Solution

by:
mjenkins earned 400 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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
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.
Suggested Courses

596 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