|
-
Nov 18th, 2005, 01:04 PM
#1
Callbacks
Does Java have a facility to execute callback functions?
-
Nov 18th, 2005, 07:48 PM
#2
Fanatic Member
Re: Callbacks
what do you mean by 'callback functions'?
-
Nov 18th, 2005, 09:22 PM
#3
Frenzied Member
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.
-
Nov 19th, 2005, 06:10 PM
#4
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.
-
Nov 26th, 2005, 02:21 PM
#5
<?="Moderator"?>
Re: Callbacks
 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?
-
Nov 26th, 2005, 02:31 PM
#6
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?
-
Nov 26th, 2005, 05:01 PM
#7
Frenzied Member
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.
-
Nov 28th, 2005, 02:31 AM
#8
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.
-
Nov 28th, 2005, 04:51 AM
#9
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;
}
.....
}
-
Nov 28th, 2005, 08:08 AM
#10
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|