This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Swing / AWT / SWT and the fly likes What is the advantage of adding custom listener to panel Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "What is the advantage of adding custom listener to panel" Watch "What is the advantage of adding custom listener to panel" New topic
Author

What is the advantage of adding custom listener to panel

Seetamraj Sriharsha
Greenhorn

Joined: Nov 09, 2009
Posts: 20
Hi,

I was taught by my lecturer on how to add custom listener to the panel itself. He actually taught me it through an example of 'Login Page'. I don't exactly remember what he said. It goes something like this, "When you have validated the username and password, it's time to say whether user is authorized to enter or not. All this happens inside the Panel itself. But how will Frame know it? Whatever happens in the panel should be known to Frame. So add your own Listener to the panel in the Frame class."

Well I'm not sure how far is this correct.

I worked on the code, It is working fine. But I can't understand the advantage of it.

Main.java



MightyFrame.java


LittlePanel.java



LordListener.java


And can someone explain this code.....

I'm passing the object of Actionlister as parameter to the mehtod addActionListener. But ActionListener is an interface (I think ) and so I can't create object.
So what is the meaning of adding curly braces and implementing the methods of ActionListener?

Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38025
    
  22
You can instantiate an interface by supplying a class body and all the methods of that interface. I haven't enough time to explain fully now, but if you search this website for "anonymous class" you will probably find something useful.

I don't understand your LordListener class. It doesn't seem to respond to any Events, so it looks like any old class. Are your custom Listeners supposed to implement a Listener interface or EventListener or similar?
pete stein
Bartender

Joined: Feb 23, 2007
Posts: 1561
I think that your Lord Listener is a very simple example of the Observer Pattern, a design pattern that is used extensively in many types of programming including and especially GUI. It allows a class to respond to events in other classes without coupling them too much -- without the two classes having to know intimate details about the other. For more on this, please look here: Observer Pattern

By understanding this subject, hopefully you'll get a better understanding of how more complex listeners (such as Swing listeners) work.
Seetamraj Sriharsha
Greenhorn

Joined: Nov 09, 2009
Posts: 20
Campbell Ritchie wrote:You can instantiate an interface by supplying a class body and all the methods of that interface. I haven't enough time to explain fully now, but if you search this website for "anonymous class" you will probably find something useful.


I think this is he answer for 2nd question. I heard about it once. But this part was very useful. Thank you very much.

Campbell Ritchie wrote:You can instantiate an interface by supplying a class body and all the methods of that interface.

Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38025
    
  22
Thank you, Pete Stein.
Seetamraj Sriharsha, Glad to be able to help
Seetamraj Sriharsha
Greenhorn

Joined: Nov 09, 2009
Posts: 20
pete stein wrote:It allows a class to respond to events in other classes without coupling them too much -- without the two classes having to know intimate details about the other.


So what does Frame class gain when it listens to the event of Panel ? That is what I understood This is passing my head now.

I'm searching for the design pattern you suggested. I'll share the points ASAP. Thanks for your kind help.
pete stein
Bartender

Joined: Feb 23, 2007
Posts: 1561
Seetamraj Sriharsha wrote:So what does Frame class gain when it listens to the event of Panel ? That is what I understood


It gains the ability to do whatever its little heart desires when one of the panel buttons is pressed, and the panel doesn't have to worry its head over just what the frame is going to do. The panel just blindly sends out a message that a button has been pressed. The frame then receiving that message could display a JOptionPane message (as per your code example above), or update a JTable in another JPanel or dialog, or again whatever the heck it wants to do.

If you didn't have a listener, and you wanted the panel to tell the JFrame to display an optionpane when a button has been pressed, the panel will have to hold a reference to the frame class somewhere and then in its code call a method of the frame class when the button has been pressed. So now you have the frame holding a reference to the panel and a panel holding a reference to the frame which == tight coupling, which is a bad thing and can make it hard to debug or extend your program.
pete stein
Bartender

Joined: Feb 23, 2007
Posts: 1561
You have to understand that the example given you is simplistic and doesn't have all the bells and whistles of a Swing Listener such as listener lists, action event parameters and such, but it still is illustrative.

Say that instead of a listener, your LittlePanel has a reference to the MightyFrame object and that currently, when a button is pressed, LittlePanel calls a method in MightyFrame that tells MightyFrame to display a JOptionPane.

Now say that you've changed MightyFrame and have given it 10 JTextFields, and now you want all the button messages displayed in the JTextFields. This means that you will have to change code in MightyFrame AND you have to change code in LittlePanel to adjust for this.

Instead, if you have a listener, all you have to do is change the code in the MightyFrame. Again, LittlePanel doesn't care what MightyFrame does with the message, only that it receives it. For e.g.,

Seetamraj Sriharsha
Greenhorn

Joined: Nov 09, 2009
Posts: 20
pete stein wrote:If you didn't have a listener, and you wanted the panel to tell the JFrame to display an optionpane when a button has been pressed, the panel will have to hold a reference to the frame class somewhere and then in its code call a method of the frame class when the button has been pressed. So now you have the frame holding a reference to the panel and a panel holding a reference to the frame which == tight coupling, which is a bad thing and can make it hard to debug or extend your program.


That is what I needed. thank you very much.....

So to conclude, if I have two panels in a frame. And if condition says that second panel components change their nature when there is change in first panel, then I can easily manage it case by this method.

I searched web and found this..... on Observer Pattern


Simply, the Observer pattern allows one object (the observer) to watch another (the subject). The Observer pattern allows the subject and observer to form a publish-subscribe relationship. Through the Observer pattern, observers can register to receive events from the subject. When the subject needs to inform its observers of an event, it simply sends the event to each observer.

For example, you might have a spreadsheet that has an underlying data model. Whenever the data model changes, the spreadsheet will need to update the spreadsheet screen and an embedded graph. In this example, the subject is the data model and the observers are the screen and graph. When the observers receive notification that the model has changes, they can update themselves.




Seetamraj Sriharsha
Greenhorn

Joined: Nov 09, 2009
Posts: 20
Everything is clear now, except the flow of execution.

I called addLordListener method in panel with class body as parameter. But it doesn't execute when the frame loads. When I called the littleTestOne() method in panel, method in frame gets executed. Keeping aside the theory I'm confused now on how the flow goes..

Can anyone please explain this, so that I can conclude this topic....Thnx in advance
Mohammed Jalal Uddin
Greenhorn

Joined: Jul 30, 2011
Posts: 1
Many many thanks. Finally I understand from this how to use custom event with a jpanel. I start learning java for 2 months.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: What is the advantage of adding custom listener to panel
 
Similar Threads
Missing Components
Facing problem with KeyListener for a frame having multiple panels
problem with ActionListener of jTextField
OK button does not dispose of parent frame within ActionListener
Java Programming Help (conversions)