Solved

Event Triggering

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

 

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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say 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

Suggested Solutions

Title # Comments Views Activity
oracle 11g 23 107
expectj telnet failing 5 45
how to debug htl and js pages 8 39
Crystal Reports Licensing Questions 4 37
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…
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…
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…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:

820 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