Solved

notificationlistener

Posted on 2014-01-28
12
219 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
 
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
print map entry 34 54
web services creation SOAP vs REST 5 19
object oriented programming comparison 5 54
even odd program using while loop 3 27
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
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…
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…

911 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

22 Experts available now in Live!

Get 1:1 Help Now