Solved

notificationlistener

Posted on 2014-01-28
12
223 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
ID: 39816517
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
ID: 39817070
I think googling for java observer pattern will get you a lot of useful links.
0
 

Author Comment

by:boukaka
ID: 39818038
The two files are attached... thanks
ODSLoader.java
ODSLoaderAppWindow.java
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 37

Accepted Solution

by:
zzynx earned 250 total points
ID: 39818150
(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
ID: 39818397
Ok, so I have to go figure out how to pass an "instance" and I think I'm good.
0
 

Author Comment

by:boukaka
ID: 39818399
Thank you very much for all your help!
0
 
LVL 35

Expert Comment

by:mccarl
ID: 39819491
@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
ID: 39820462
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
ID: 39828175
Oh sorry, just noticed this. Where to I ask for a re-open?
Thanks,
0
 

Author Closing Comment

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

Expert Comment

by:mccarl
ID: 39831329
No worries, thanks for resolving it! :)
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
ejb mdb examples 1 19
SequenceInputStream example 3 21
how to debug htl and js pages 8 53
Java array 10 54
By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
The viewer will learn how to implement Singleton Design Pattern in Java.
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.

679 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