Solved

Event Triggering

Posted on 1998-07-18
6
279 Views
Last Modified: 2010-03-30
I'm trying to post an event to the queue from an applet method and have the action method evaluate it just as it would if it were a user input (ex button click).  Can anyone modify the following code so that win.eval("test") works in the action method whenever the callback method is called:

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

 // * Please Ignore Irrelavant Parts *

public class MyApplet extends Applet implements EventOutObserver {

  Event event1;
  TextField tf;

// * (Extra Code) *

  public void init() {

// * (Extra Code) *

    tf = new TextField("Text field", 30);      // I'm using TextField as a dummy awt component.
    add(tf);
    event1 = new Event(tf,9977,tf);            // This is a dummy event which I'm trying to send to                                                                                                                                                                                                                    // activate the action method.

//                *                        *
//                *                        *              
//                * (Main Code) *
//                *                        *
//                *                        *

 public void callback(EventOut value, double timestamp, Object userData) {

       output.appendText("\nEAI.callback()...x\n");
       musicPlaying(event1,tf);

 }

 public boolean musicPlaying(Event evt, Object what) {

         output.appendText("musicPlaying() = " + "\n");

         if (musicon == 0) {
                   output.appendText("Music now Playing!...\n");
                   musicon = 1;
                   event1.id = 9977;
                   event1.arg = tf;
                   postEvent(event1);
           tf.setText("Music On");
           handleEvent(event1);
                   return true;
         } else {
                   output.appendText("Music now Paused!...\n");
                   musicon = 0;
           event1.id = 9977;
                   event1.arg = tf;
           postEvent(event1);
            tf.setText("Music Off");
                   handleEvent(event1);
                   return true;
      }      

 }

  public boolean handleEvent(Event evt) {
           switch (evt.id) {
                  case Event.ACTION_EVENT:
                           return action(evt, evt.arg);
      case 9977:
                           return action(evt, evt.arg);                       
               // other case elements omitted
                  default:
                           return false;
      }
  }

  public boolean action(Event event, Object what) {

          int idone = event.id;
          output.appendText("\n Event id =" + idone +"\n");
      
          if (error) {
                 showStatus("Problems! Had an error during initialization");
                 return true;  
          }      

          if (event.target instanceof Button) {
                 Button b = (Button) event.target;
                 if (b.getLabel() == "Test!") {
             win.eval("test()");
                 }
                 if (b.getLabel() == "Sound") {
             win.eval("startGroovoid('Fanfare-Cascade')");
                 }
          } else {
                 win.eval("test()");
          }
          return true;
  }

**************************************************************************
I am not sure if I correctly created the event much less how it should be triggered or initiated by the callback method and recognized by the action method.  The predefined awt components do all of the event handling "underground".

I'd be very grateful for any suggestions.

0
Comment
Question by:LordVader
[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
  • 5
6 Comments
 

Author Comment

by:LordVader
ID: 1227003
Edited text of question
0
 

Author Comment

by:LordVader
ID: 1227004
Edited text of question
0
 

Author Comment

by:LordVader
ID: 1227005
Edited text of question
0
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!

 

Author Comment

by:LordVader
ID: 1227006
Edited text of question
0
 
LVL 16

Accepted Solution

by:
imladris earned 100 total points
ID: 1227007
The creation of the Event is OK (although the last argument is intended to be an argument, and null would probably be more appropriate).

Now it needs to go somewhere. AWT components have method for inserting the event into the handling stream:

public boolean postEvent(Event e);

Writing:

postEvent(event1);

will insert it into the event handling stream.

0
 

Author Comment

by:LordVader
ID: 1227008
It is calling the action method now, however, I have a new problem:  The Action method does not see the event as an exact equal to an event initiated by a user.

For example if I change the text in the text field, win.eval("test()"); is properly called.  If an instance of the callback method occurs, the action method initiates, then freezes when calling win.eval("test()").

The only modifications I've made were:

    event1 = new Event(tf,1001,null);

 public boolean musicPlaying(Event evt, Object what) {

       if (musicon == 0) {
            output.appendText("Music now Playing!...\n");
            musicon = 1;
            postEvent(event1);
            tf.setText("Music On");
          handleEvent(event1);
            return true;
        } else {
            output.appendText("Music now Paused!...\n");
            musicon = 0;
          postEvent(event1);
           tf.setText("Music Off");
          handleEvent(event1);
            return true;
      }      
 }

Any Ideas as to what I could modify or add to recreate an exact user event?

Thanks ahead of time if you can help me out.

0

Featured Post

Enroll in July's Course of the Month

July's Course of the Month is now available! Enroll to learn HTML5 and prepare for certification. It's free for Premium Members, Team Accounts, and Qualified Experts.

Question has a verified solution.

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

Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
The viewer will learn how to implement Singleton Design Pattern in Java.
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
Suggested Courses

626 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