Obviously this is more of a Java programming question that a Jess
question, but it's worth answering because it's the kind of Java
question that flummoxes many people until someone finally explains
it...

What you want to do, whether you know it or not, is send a signal
between two threads. You are calling get-return-value in one thread,
while the AWT is dispatching your ActionEvent on another
thread. The method that Java provides for sending inter-thread
messages is Object.notify(), and, basically, here's what you need to
do:

class popupwindow implements ActionListener
{
  private static final int NO_RESULT = -1;
  private int m_result = NO_RESULT;

  // "synchronized" is important here. We're using the popupwindow
  // object itself as the monitor through which we send the message.
  // If this class doesn't implement ActionListener itself, you would
  // have to do this slightly differently. For instance, if
  // actionPerformed were in an anonymous inner class, then instead of
  // actionPerformed being synchronized, you'd have a synchronized
  // block with popupwindow.this being the argument and call
  // popupwindow.this.notify().

  private synchronized int getResult() throws InterruptedException
  {
   // This will probably only call wait() once, but good style
   // dictates that you use a loop. wait() returns when someone
   // calls notify(). No busy-waiting or polling needed.
    while (m_result == NO_RESULT)        
      wait();
    return m_result;
  }

  public synchronized void actionPerformed(ActionEvent ae)
  {
    // Determine result however
    m_result = getResultFromEvent(ae);
    notify();
  }
  ...
}


Have fun!

I think Neil Chinnery wrote:
> Hi,
> 
> I am fairly new to the whole gui/event-driven model of programming and am 
> hoping that someone can offer me some advice.
> 
> I've written a class in java that when called , displays a 'pop-up' window 
> with a number of buttons on it.  When a user hits a button, a value is 
> returned.
> 
> I've integrated this into Jess in the following way:
> 
> (bind ?x (new popupwindow))
> (bind ?y (call ?x get-return-value))
> 
> Where 'get-return-value' is the method within the class 'popupwindow' that 
> waits for the user to click on a button.
> 
> When I first implemented this, the 2nd line of the above code would fire off 
> the method that displays the window and then immediately continue with the 
> next jess program statement.
> 
> I understand why it does this, but what I want to happen is that the jess 
> program execution is halted until a user has pressed a button on the pop up 
> window.
> 
> The only way I have managed to do this is to put a holding loop in the  java 
> method which waits until a user has clicked a button.  The code for this 
> loop is as follows:
> 
> while (retval==0)
> {
>   try {
>        Thread.sleep(1500);
>   }
>   catch (InterruptedException e)
>   {
>     return 0;
>   }
> }
> 
> return retval;
> 
> Within the ActionListener portion of the button code, if a user hits a 
> button the variable retval is modified to an integer value.
> 
> Anyway, my question is, is there a more elegant way of doing this?  It seems 
> to me to be a bit of kludge, but I am unsure of what else I can do.  Any 
> help would be really appreciated.
> 
> thanks
> 
> Neil
> ________________________________________________________________________
> Get Your Private, Free E-mail from MSN Hotmail at http://www.hotmail.com
> 
> ---------------------------------------------------------------------
> To unsubscribe, send the words 'unsubscribe jess-users [EMAIL PROTECTED]'
> in the BODY of a message to [EMAIL PROTECTED], NOT to the
> list (use your own address!) List problems? Notify [EMAIL PROTECTED]
> ---------------------------------------------------------------------
> 



---------------------------------------------------------
Ernest Friedman-Hill  
Distributed Systems Research        Phone: (925) 294-2154
Sandia National Labs                FAX:   (925) 294-2234
Org. 8920, MS 9012                  [EMAIL PROTECTED]
PO Box 969                  http://herzberg.ca.sandia.gov
Livermore, CA 94550
---------------------------------------------------------------------
To unsubscribe, send the words 'unsubscribe jess-users [EMAIL PROTECTED]'
in the BODY of a message to [EMAIL PROTECTED], NOT to the
list (use your own address!) List problems? Notify [EMAIL PROTECTED]
---------------------------------------------------------------------

Reply via email to