41

I want to know about how to set our own Listeners in java.For example I have a function that increments number from 1 to 100. i want to set a listener when the value reaches 50. How can i do that? Pls suggest me any tutorial.

Dimitri
8,31819 gold badges75 silver badges132 bronze badges
asked Aug 4, 2011 at 12:23
2

5 Answers 5

33

https://stackoverflow.com/a/6270150/3675925

You probably want to look into the observer pattern.

Here's some sample code to get you started:

import java.util.*;
// An interface to be implemented by everyone interested in "Hello" events
interface HelloListener {
 void someoneSaidHello();
}
// Someone who says "Hello"
class Initiater {
 private List<HelloListener> listeners = new ArrayList<HelloListener>();
 public void addListener(HelloListener toAdd) {
 listeners.add(toAdd);
 }
 public void sayHello() {
 System.out.println("Hello!!");
 // Notify everybody that may be interested.
 for (HelloListener hl : listeners)
 hl.someoneSaidHello();
 }
}
// Someone interested in "Hello" events
class Responder implements HelloListener {
 @Override
 public void someoneSaidHello() {
 System.out.println("Hello there...");
 }
}
class Test {
 public static void main(String[] args) {
 Initiater initiater = new Initiater();
 Responder responder = new Responder();
 initiater.addListener(responder);
 initiater.sayHello(); // Prints "Hello!!!" and "Hello there..."
 }
}
answered Apr 16, 2016 at 10:24
1
26

Have a look at the source of any class that uses listeners. In fact it's quite easy:

  • create an interface for your listener, e.g. MyListener
  • maintain a list of MyListener
  • upon each event that the listeners should listen to, iterate over the list and call the appropriate method with some event parameter(s)

As for the observer pattern along with some Java code have a look at wikipedia.

answered Aug 4, 2011 at 12:27
0
0

There is no built-in mechanism that would allow you to attach listeners to all variables. The object you want to watch needs to provide the support for that by itself. For example it could become Observable and fire off onChange events to its Observers (which you also have to ensure are being tracked).

answered Aug 4, 2011 at 12:29
0

I would recommend using EventBus for your use case. It has a nice API design and is easy to use. Have a look at their Getting Started section to see how it works.

answered Aug 4, 2011 at 12:36
1
0

You can use the Signals library for it. It will look like that:

interface FiftySignal{
 void on50();
}
class FiftyMaker{
 FiftySignal fiftySignal = Signals.signal(FiftySignal.class).dispatcher;
 void doIt(){
 for(int i = 0; i < 100; i++){
 if(i == 50){
 fiftySignal.on50(); // dispatching the event
 }
 } 
 }
}
class Boo{
 Signal<FiftySignal> fiftySignal = Signals.signal(FiftySignal.class);
 void bar(){
 fiftySignal.addListener(()-> System.out.println("It's 50!")); // listener
 }
}

Disclaimer: I am the author of Signals.

answered Mar 16, 2021 at 2:21

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.