Solved

Event Triggering

Posted on 1998-07-18
6
273 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
  • 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
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.

 

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

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

This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
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…

929 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

14 Experts available now in Live!

Get 1:1 Help Now