Results 1 to 5 of 5
  1. #1
    Bibhukalyana is offline Member
    Join Date
    Apr 2013
    Posts
    4
    Rep Power
    0

    Default Non blocking callback method

    Hi everyone,
    I want to use a non blocking callback method.
    I have two class A and B. I want to call a method of B from A.But I do not want to wait any methods of A after calling the call back method.
    Java Code:
    interface CallBack
    {
    	void callBackMethod(int val);
    }
    
    public class ClassA 
    {
    	private CallBack callback;
    	public ClassA(CallBack callback)
    	{
    		this.callback = callback;
    	}
    	
    	public void test()
    	{
    		int count = 10;
    		while(count > 0)
    		{
    			callback.callBackMethod(count);
    			System.out.print(count);
    			count--;
    		}
    	}
    }
    
    public class ClassB 
    {
    
    	public ClassB()
    	{
    		ClassA objA = new ClassA(new CallBack() {
    			
    			@Override
    			public void callBackMethod(int arg0) {
    				// TODO Auto-generated method stub
    				JOptionPane.showMessageDialog(null, "Called", "called", JOptionPane.ERROR_MESSAGE);
    			}
    		});
    		
    		objA.test();
    	}
    	
    	public static void main(String[] args) 
    	{
    		ClassB objB = new ClassB();
    	}
    
    }

    Here fast message box of B will appear and after pressing OK the print will executed.

    But i want something that no matter you press OK or not print statement will executed without waiting.

    Something i want is asynchronous callback.

    Please help me.

    Thanks.

  2. #2
    wsaryada is offline Senior Member
    Join Date
    Jun 2007
    Location
    Bali, Indonesia
    Posts
    760
    Rep Power
    8

    Default Re: Non blocking callback method

    The following document says: How to Make Dialogs (The Java™ Tutorials > Creating a GUI With JFC/Swing > Using Swing Components)

    When a modal Dialog is visible, it blocks user input to all other windows in the program. JOptionPane creates JDialogs that are modal. To create a non-modal Dialog, you must use the JDialog class directly.
    To continue your program flow without the JOptionPane block the exexution you have to use a JDialog. Or you can still use a JOptionPane but you should create the JOptionPane in its own Thread instead.

    Java Code:
    @Override
    public void callBackMethod(int arg0) {
        Thread thread = new Thread(new Runnable() {
            public void run() {
                JOptionPane.showMessageDialog(null, "Called", "called", JOptionPane.ERROR_MESSAGE);
            }
        });
        thread.start();     
    }
    Last edited by wsaryada; 04-11-2013 at 05:34 PM.

  3. #3
    Bibhukalyana is offline Member
    Join Date
    Apr 2013
    Posts
    4
    Rep Power
    0

    Default Re: Non blocking callback method

    Thanks for reply.
    Is it possible without thread ?

  4. #4
    wsaryada is offline Senior Member
    Join Date
    Jun 2007
    Location
    Bali, Indonesia
    Posts
    760
    Rep Power
    8

    Default Re: Non blocking callback method

    Try to use JDialog instead of JOptionPane.

  5. #5
    doWhile is offline Moderator
    Join Date
    Jul 2010
    Location
    California
    Posts
    1,641
    Rep Power
    7

    Default Re: Non blocking callback method

    Quote Originally Posted by wsaryada View Post
    Or you can still use a JOptionPane but you should create the JOptionPane in its own Thread instead.
    I presume the examples using JOptionPane are for demonstration purposes, but using Swing in this context does complicate things due to the single threaded model - calls to Swing should be dispatched to the EDT rather than made from within other threads.

    Is it possible without thread ?
    If you wish to perform 2 things in parallel, you must multi-thread. And for what its worth, if you are calling the method many times you should look into using thread pools rather than having each call its own thread

Similar Threads

  1. use api without callback url?
    By Seiya0890 in forum New To Java
    Replies: 0
    Last Post: 03-02-2013, 05:02 PM
  2. Block thread until callback method has been called by other thread
    By James2000 in forum Threads and Synchronization
    Replies: 4
    Last Post: 05-11-2012, 09:46 PM
  3. query with a callback?
    By Filobel in forum JDBC
    Replies: 4
    Last Post: 01-20-2012, 09:33 AM
  4. Override Entity Callback Method
    By CatchSandeepVaid in forum Enterprise JavaBeans (EJB)
    Replies: 0
    Last Post: 12-13-2009, 06:05 AM
  5. Setting a timeinterval callback ???
    By wallys in forum New To Java
    Replies: 1
    Last Post: 08-19-2008, 05:52 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •