Solved

notificationlistener

Posted on 2014-01-28
12
218 Views
Last Modified: 2014-02-03
Does anyone know of a good tutorial for beginners on how to create an interface with a notification listener that listens to a class and how to observe it from another class?

I have a class called ODSLoader that I need to listen to - I have the basic structure of the interface (I think) but I have no idea how to code it to actually listen to ODSLoader.

public interface Notifier {
         public interface NotificationListener {
             public void onNotificationPosted();           
            
         }

         public void addListener(NotificationListener listener);
         public void removeListener(NotificationListener listener);
      }

I then have another class with a progress bar that needs to be notified when to progress but I have no idea how to do that either...

This is my progress bar

            progressBar = new JProgressBar(0,100);
            progressBar.setValue(0);
            springLayout.putConstraint(SpringLayout.NORTH, progressBar, 12, SpringLayout.SOUTH, btnProcess);
            springLayout.putConstraint(SpringLayout.WEST, progressBar, 10, SpringLayout.WEST, frame.getContentPane());
            springLayout.putConstraint(SpringLayout.EAST, progressBar, -10, SpringLayout.EAST, frame.getContentPane());
            frame.getContentPane().add(progressBar);

Can anyone point me in the right direction or show me how to code it?
Thanks
0
Comment
Question by:boukaka
  • 5
  • 3
  • 3
12 Comments
 
LVL 35

Assisted Solution

by:mccarl
mccarl earned 250 total points
Comment Utility
In very basic terms you would code ODSLoader something like this...
public class ODSLoader implements Notifier {
    private List<Notifier.NotificationListener> listeners = new ArrayList<Notifier.NotificationListener>();

    public void addListener(Notifier.NotificationListener listener) {
        listeners.add(listener);
    }
    public void removeListener(Notifier.NotificationListener listener) {
        listeners.remove(listener);
    }
    
    private void notifyListeners() {
        for (Notifier.NotificationListener listener : listeners) {
            listener.onNotificationPosted();
        }
    }

    // All the normal ODSLoader code follows...
    // But where something interesting happens in the ODSLoader code, you insert a...
    notifyListeners();
    // call that calls the above method to send the notifications to all listeners
}

Open in new window

Now all you have to do, is somewhere in the code that creates (or has access to) "progressBar", you can make that class implement "Notifier.NotificationListener" and then you write the "onNotificationPosted()" method and in that method you can update your progressBar. Finally, in that same code you would need to register the listener, by calling the ODSLoader's addListener() method and passing it the instance of the class that implements "Notifier.NotificationListener".

If the above is not clear enough, can you post more of your code? Post (at the very least) your ODSLoader class and the class that creates your ProgressBar.
0
 
LVL 37

Expert Comment

by:zzynx
Comment Utility
I think googling for java observer pattern will get you a lot of useful links.
0
 

Author Comment

by:boukaka
Comment Utility
The two files are attached... thanks
ODSLoader.java
ODSLoaderAppWindow.java
0
 
LVL 37

Accepted Solution

by:
zzynx earned 250 total points
Comment Utility
(1) Since you have
progressBar = new JProgressBar(0,5);

Open in new window

I suppose you have 5 big parts that ODSLoader has to perform.
With the code mccarl already provided, you have to call
notifyListeners();

Open in new window

at the end of each of those parts.

(2) Then in ODSLoaderAppWindow you replace the commented part

	//	public static void progress() {
	 //       progressBar.setValue(progressBar.getValue() + 1);
	 //   }

Open in new window

by
public void onNotificationPosted() {
       progressBar.setValue(progressBar.getValue() + 1);
}

Open in new window


(3) At last, of course, you have to make your ODSLoaderAppWindow listen to ODSLoader by having in your ODSLoaderAppWindow:
myODSLoaderInstance.addListener(this);

Open in new window


So, somehow you'll have to pass the instance of your ODSLoader java class (don't know where you create it) to your ODSLoaderAppWindow
0
 

Author Comment

by:boukaka
Comment Utility
Ok, so I have to go figure out how to pass an "instance" and I think I'm good.
0
Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

 

Author Comment

by:boukaka
Comment Utility
Thank you very much for all your help!
0
 
LVL 35

Expert Comment

by:mccarl
Comment Utility
@boukaka,

I gave you quite a bit of the code that you needed in my first comment, and zzynx followed up with some more in another comment.

Do you think that the points should have been split between the two of us?
0
 
LVL 37

Expert Comment

by:zzynx
Comment Utility
I agree that a split would have been more fair.
You can ask for a reopen of this question and once it is, you can close it again giving both of us points.
0
 

Author Comment

by:boukaka
Comment Utility
Oh sorry, just noticed this. Where to I ask for a re-open?
Thanks,
0
 

Author Closing Comment

by:boukaka
Comment Utility
Points have been split 50/50 now. Sorry about that.
0
 
LVL 35

Expert Comment

by:mccarl
Comment Utility
No worries, thanks for resolving it! :)
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now