Link to home
Start Free TrialLog in
Avatar of srivenky
srivenkyFlag for United States of America

asked on

setCallBack method in swing

Could some one explain me about setCallBack method in swing
SOLUTION
Avatar of TimYates
TimYates
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of srivenky

ASKER

If you could explain me the concept behind it, it would be more useful.
Say you define an interface:

---------

interface GuffCallback
{
  public void doSomething( String woo ) ;
}

---------

Then, you can make another class that will call you back when it is done (in this case, a threaded object)

---------

public class ThreadedObject implements Runnable
{
  volatile Thread runner = null ;
  GuffCallback whenDone = null ;

  public ThreadedObject()
  {
  }

  public void setCallback( GuffCallback cb )
  {
    whenDone = cb ;
  }

  public void start()
  {
    if( runner == null )
    {
      runner = new Thread( this ) ;
      runner.start() ;
    }
  }
 
  public void run()
  {
    // do something that will take some time...
    try{ runner.sleep( 30000 ) ; } catch( InterruptedException ex ) {}
    if( whenDone != null )
    {
      whenDone.doSomething( "DONE IT!!" ) ;
    }
  }
}

---------

Then, you make your main class implement that interface, and create the thread thing to call you back...

---------

public MyMainClass extends JFrame implements GuffCallback
{

  ....
  // required for the interface...
  public void doSomething( String woo )
  {
    System.out.println( "Something called with parameter " + woo ) ;
  }
  ...

  public void doIt()
  {
    ThreadedObject obj = new ThreadedObject() ;
    obj.setCallback( this ) ;
    obj.start() ;
  }
}

---------
Avatar of CleanupPing
CleanupPing

srivenky:
This old question needs to be finalized -- accept an answer, split points, or get a refund.  For information on your options, please click here-> http:/help/closing.jsp#1 
EXPERTS:
Post your closing recommendations!  No comment means you don't care.
please delete this question
No comment has been added lately, so it's time to clean up this TA.
I will leave a recommendation in the Cleanup topic area that this question is:

- Split points between thanassis and TimYates

Please leave any comments here within the next seven days.

PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!

girionis
EE Cleanup Volunteer