srivenky
asked on
setCallBack method in swing
Could some one explain me about setCallBack method in swing
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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() ;
}
}
---------
---------
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() ;
}
}
---------
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.
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.
ASKER
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
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
ASKER