Does Java have a facility to execute callback functions? :ehh:
Printable View
Does Java have a facility to execute callback functions? :ehh:
what do you mean by 'callback functions'?
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.
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?Quote:
Originally Posted by CornedBee
http://java.sun.com/developer/techni...curity/jaasv2/
Or is this just something that is only implemented in the security modules of Java?
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?
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
Something like that? If not, maybe CB will help you.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
}
}
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.
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;
}
.....
}
Alright. The short version.
This hypothetical code (C function pointer syntax, disregarding the this pointer problem):
is conceptually equivalent to this code: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);
}
}
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);
}
});
}
}