Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Bind event/callback to keyword in Java

I am new in Java coming from javascript background.

In javascript , we can save callbacks in objects like this-

//we register some callback on a keyword
    var observerCallbacks = {};
     registerObserverCallback = function(eventname ,callback){
                observerCallbacks[eventname] =  callback;
            };
//For calling events
    notifyObservers = function(eventname){         
         observerCallbacks[eventname]();                               
            };

Usage :

//Register event 
      registerObserverCallback('eventA',function(){
          //body of callback  
     });

//For calling event
notifyObservers('eventA');

I am looking for equivalent functionality in Java. Is there way of creating this type of custom functionality.

like image 255
NeiL Avatar asked Sep 28 '22 11:09

NeiL


2 Answers

Java has a slightly different mechanism for this purpose. A object registers itself as a listener to specific type of event When this kind of event occurs, all listeners are notified.

Here you can find a tutorial about it. Below code is from there.

public class MultiListener ... implements ActionListener {
    ...
    //where initialization occurs:
        button1.addActionListener(this);
        button2.addActionListener(this);

        button2.addActionListener(new Eavesdropper(bottomTextArea));
    }

    public void actionPerformed(ActionEvent e) {
        topTextArea.append(e.getActionCommand() + newline);
    }
}

class Eavesdropper implements ActionListener {
    ...
    public void actionPerformed(ActionEvent e) {
        myTextArea.append(e.getActionCommand() + newline);
    }
}

More on this topic could be found here

like image 98
Alp Avatar answered Sep 30 '22 13:09

Alp


JavaScript is an asynchronous language, Java is a synchronous language. What you want to do is the same as asking "How to I turn my hammer into a screwdriver?" While it's still fully possible with creativity to make the hammer screw something in, it's not how the tool is intended to be used and the result may vary.

The callbacks are essential in JavaScript to let your program know that the function has completed, Java knows that the function is completed since it stops to execute it.

Java offers you event listeners for events that may seem similar when you don't know better; they lay idle until whatever they're listening for activates them, then it activates, executes and goes back to listening.

The Java way of doing this is to tell your listener to do whatever you want it to do straight away; you don't stop to wait for a callback because Java will stop, execute what you tell it to, then go back into the place where it came from. How to write the code for a listener can be found in the oracle tutorial but your issue is more of a logic issue rather than the specific code.

If you want the listener to store something for another method, a bean with getters and setters would be one Java way.

like image 21
Gemtastic Avatar answered Sep 30 '22 14:09

Gemtastic