Results 1 to 10 of 10

Thread: Callbacks

  1. #1

    Thread Starter
    VBA Nutter visualAd's Avatar
    Join Date
    Apr 2002
    Location
    Ickenham, UK
    Posts
    4,906

    Callbacks

    Does Java have a facility to execute callback functions?
    PHP || MySql || Apache || Get Firefox || OpenOffice.org || Click || Slap ILMV || 1337 c0d || GotoMyPc For FREE! Part 1, Part 2

    | PHP Session --> Database Handler * Custom Error Handler * Installing PHP * HTML Form Handler * PHP 5 OOP * Using XML * Ajax * Xslt | VB6 Winsock - HTTP POST / GET * Winsock - HTTP File Upload

    Latest quote: crptcblade - VB6 executables can't be decompiled, only disassembled. And the disassembled code is even less useful than I am.

    Random VisualAd: Blog - Latest Post: When the Internet becomes Electricity!!


    Spread happiness and joy. Rate good posts.

  2. #2
    Fanatic Member x-ice's Avatar
    Join Date
    Mar 2004
    Location
    UK
    Posts
    671

    Re: Callbacks

    what do you mean by 'callback functions'?

  3. #3
    Frenzied Member System_Error's Avatar
    Join Date
    Apr 2004
    Posts
    1,111

    Re: Callbacks

    Not that I know of. You could encapsulate your callback function in an object and pass that as an argument.


    NOTE: You could take a look at JNI. It might have them, it might not. However, I'm sure you can download a 3rd party package that supports it, but I wouldn't see the point in doing that.

  4. #4
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594

    Re: Callbacks

    Java doesn't have callbacks or delegates. It has listeners: usually, these are tiny interfaces (1-4 methods) that you implement and pass that object to whoever should call you back.
    Code:
    JButton button = new JButton("Hit Me");
    button.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            JOptionPane.showMessageBox("Whee!");
        }
    });
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  5. #5
    <?="Moderator"?> john tindell's Avatar
    Join Date
    Jan 2002
    Location
    Brighton, UK
    Posts
    1,099

    Re: Callbacks

    Quote Originally Posted by CornedBee
    Java doesn't have callbacks or delegates. It has listeners: usually, these are tiny interfaces (1-4 methods) that you implement and pass that object to whoever should call you back.
    Code:
    JButton button = new JButton("Hit Me");
    button.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            JOptionPane.showMessageBox("Whee!");
        }
    });
    Reading about the security within Java and part of it uses "javax.security.auth.callback" Cannot something like this be translated across?

    http://java.sun.com/developer/techni...curity/jaasv2/

    Or is this just something that is only implemented in the security modules of Java?

  6. #6

    Thread Starter
    VBA Nutter visualAd's Avatar
    Join Date
    Apr 2002
    Location
    Ickenham, UK
    Posts
    4,906

    Re: Callbacks

    The reason I ask, is becasuse I am making a menu generator for the console. Each Menu, will have one of more MenuItem. It would be nice if the Menu object, could receive a response from the user and ivoke a callback associated with the particular MenuItem for the choice made.

    Would it be possible to acheive this with listeners?
    PHP || MySql || Apache || Get Firefox || OpenOffice.org || Click || Slap ILMV || 1337 c0d || GotoMyPc For FREE! Part 1, Part 2

    | PHP Session --> Database Handler * Custom Error Handler * Installing PHP * HTML Form Handler * PHP 5 OOP * Using XML * Ajax * Xslt | VB6 Winsock - HTTP POST / GET * Winsock - HTTP File Upload

    Latest quote: crptcblade - VB6 executables can't be decompiled, only disassembled. And the disassembled code is even less useful than I am.

    Random VisualAd: Blog - Latest Post: When the Internet becomes Electricity!!


    Spread happiness and joy. Rate good posts.

  7. #7
    Frenzied Member System_Error's Avatar
    Join Date
    Apr 2004
    Posts
    1,111

    Re: Callbacks

    If I understand correctly it should be. Simply add an actionlistener to the menu items, and process them in an actionperformed method.

    JMenu fileMenu;
    JMenuItem openItem;
    JMenuItem saveItem;
    JMenuItem exitItem;

    //add the actionlistener

    Code:
    public void actionPerformed(ActionEvent ae)
    {
      if (ae.getSource() == openItem)
      {
         //process open
      }
      else if (ae.getSource() == saveItem)
      {
       //process save
      }
      else if (ae.getSource() == exitItem)
      {
        //process exit
      }
    }
    Something like that? If not, maybe CB will help you.

  8. #8
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594

    Re: Callbacks

    I fail to see the problem. I suggest you do a Swing tutorial to learn how listeners work. They're practically identical to normal callbacks.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  9. #9

    Thread Starter
    VBA Nutter visualAd's Avatar
    Join Date
    Apr 2002
    Location
    Ickenham, UK
    Posts
    4,906

    Re: Callbacks

    For the moment I have created a mini interface like a listener to pass to the MenuItem object as a callback. I did have a quick look at swing, but decided against it for this as I only need a one callback function and given the time constraints I have on this project I would not have time to fully understand it before using it.
    Code:
    interface MenuItemCallback
    {
    	public void callback();
    }
    
    public class MenuItem
    {
    	private Menu owner;
    	private Menu subMenu;
    	private String displayName;
    	private char accessChar;
    	private MenuItemCallback callback;
    	
    	MenuItem(Menu owner, String displayName, char accessChar)
    	{
    		this(owner, displayName, accessChar, (Menu) null, (MenuItemCallback) null);
    	}
    	
    	MenuItem(Menu owner, String displayName, char accessChar, Menu subMenu)
    	{
    		this(owner, displayName, accessChar, subMenu, (MenuItemCallback) null);
    	}
    
    
    	MenuItem(Menu owner, String displayName, char accessChar, MenuItemCallback callback)
    	{
    		this(owner, displayName, accessChar, (Menu) null, callback);
    	}
    	
    	MenuItem(Menu owner, String displayName, char accessChar, Menu subMenu, MenuItemCallback callback)
    	{
    		this.owner = owner;
    		this.displayName = displayName;
    		this.accessChar = accessChar;
    		this.subMenu = subMenu;
    		this.callback = callback;
    	}
    
            .....
    }
    PHP || MySql || Apache || Get Firefox || OpenOffice.org || Click || Slap ILMV || 1337 c0d || GotoMyPc For FREE! Part 1, Part 2

    | PHP Session --> Database Handler * Custom Error Handler * Installing PHP * HTML Form Handler * PHP 5 OOP * Using XML * Ajax * Xslt | VB6 Winsock - HTTP POST / GET * Winsock - HTTP File Upload

    Latest quote: crptcblade - VB6 executables can't be decompiled, only disassembled. And the disassembled code is even less useful than I am.

    Random VisualAd: Blog - Latest Post: When the Internet becomes Electricity!!


    Spread happiness and joy. Rate good posts.

  10. #10
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594

    Re: Callbacks

    Alright. The short version.

    This hypothetical code (C function pointer syntax, disregarding the this pointer problem):
    Code:
    class WithCallback
    {
      private void (*actionPerformedHandler)(ActionEvent);
    
      public void setCallback(void (*handler)(ActionEvent)) {
        actionPerformedHandler = handler;
      }
    
      protected void fireCallback()  {
        actionPerformedHandler(new ActionEvent(this));
      }
    }
    
    class SuppliesCallback
    {
      public void onClick(ActionEvent e) {
        // Whatever
      }
    
      public void init() {
        wantsCallback.setCallback(onClick);
      }
    }
    is conceptually equivalent to this code:
    Code:
    interface ActionListener
    {
      public void actionPerformed(ActionEvent e);
    }
    
    class WithCallback
    {
      private ActionListener actionPerformedHandler;
    
      public void setCallback(ActionListener handler) {
        actionPerformedHandler = handler;
      }
    
      protected void fireCallback()  {
        actionPerformedHandler.actionPerformed(new ActionEvent(this));
      }
    }
    
    class SuppliesCallback
    {
      public void onClick(ActionEvent e) {
        // Whatever
      }
    
      public void init() {
        wantsCallback.setCallback(new ActionListener() {
          public void actionPerformed(ActionEvent e) {
            onClick(e);
          }
        });
      }
    }
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

Posting Permissions

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



Click Here to Expand Forum to Full Width