Solved

Event Triggering

Posted on 1998-07-18
6
272 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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 

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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Java 1603 Error 2 42
Modeling a class in java 5 31
for i loop in grovy 1 32
Java - Why doesn't this JFrame work 3 16
Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
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…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:

708 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

12 Experts available now in Live!

Get 1:1 Help Now