• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

What is the advantage of adding custom listener to panel

 
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?

 
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Bartender
Posts: 1561
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you, Pete Stein.
Seetamraj Sriharsha, Glad to be able to help
 
Seetamraj Sriharsha
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 1561
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 1561
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Many many thanks. Finally I understand from this how to use custom event with a jpanel. I start learning java for 2 months.
 
See where your hand is? Not there. It's next to this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic