Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Event Triggering

Posted on 1998-07-18
6
Medium Priority
?
286 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
Technology Partners: 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 200 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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

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…
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…
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
This video teaches viewers about errors in exception handling.
Suggested Courses

877 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