Solved

notificationlistener

Posted on 2014-01-28
12
221 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
Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

 
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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

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…
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 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:
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:

785 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