Solved

notificationlistener

Posted on 2014-01-28
12
234 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 3
  • 3
12 Comments
 
LVL 36

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
PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

 
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 36

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 36

Expert Comment

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

Featured Post

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!

Question has a verified solution.

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

INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
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:
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
Suggested Courses

617 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