Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 7 of 7

Thread: Event Handling - Best Practice?

  1. #1
    Junior Member
    Join Date
    Sep 2011
    Posts
    10
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Default Event Handling - Best Practice?

    Greetings All

    I am a C++ programmer who is attempting to learn Java with the ultimate aim of sitting the Oracle certification. I have (hopefully) a very simply question.

    What is the best method for implementing listeners? Should I be putting them into anonymous inner classes or into a fully fledged private inner class? I have googled this question but my efforts haven't yielded a definite answer.

    Many Thanks
    Nikki


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,264
    Thanks
    176
    Thanked 821 Times in 764 Posts
    Blog Entries
    5

    Default Re: Event Handling - Best Practice?

    In my opinion there really isn't a best method. I've seen listeners implemented in a large number of ways...many with a different reason behind the implementation. My .02 - always think about code re-usability and adaptability to change. Sometimes writing inner classes locks you into an implementation that is difficult to modify in the future, in other situations it encapsulates the implementation details which can be beneficial - both are case dependent.

  3. The Following User Says Thank You to copeg For This Useful Post:

    ishtar (October 5th, 2011)

  4. #3
    Junior Member
    Join Date
    Jan 2011
    Posts
    15
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Event Handling - Best Practice?

    It depends if listener has to be used by multiple object than making it a full fleged class make sense but if its only attached to one element than having them as inner class is ok.

  5. #4
    Member
    Join Date
    Jun 2011
    Posts
    56
    Thanks
    2
    Thanked 7 Times in 6 Posts

    Default Re: Event Handling - Best Practice?

    Oracle suggests use of inner classes for listeners. Switch statements are not good OOP design .
    On the downside, you will start having tons of .class files for each event handler. That could ,for example, affect loading time of applets. But until then, it is better to go with the suggestion.

  6. The Following 2 Users Say Thank You to dabdi For This Useful Post:

    ishtar (October 5th, 2011), kyasuleyman (May 1st, 2012)

  7. #5
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,264
    Thanks
    176
    Thanked 821 Times in 764 Posts
    Blog Entries
    5

    Default Re: Event Handling - Best Practice?

    Oracle suggests use of inner classes for listeners.
    Curious where this is suggested. Do you have a link? Because from the standpoint of making code loosely couple, adaptable, and reusable this advice needs to be scrutinized.
    Last edited by copeg; October 1st, 2011 at 01:55 PM.

  8. The Following User Says Thank You to copeg For This Useful Post:

    ishtar (October 5th, 2011)

  9. #6
    Member
    Join Date
    Jun 2011
    Posts
    56
    Thanks
    2
    Thanked 7 Times in 6 Posts

    Default Re: Event Handling - Best Practice?

    Quote Originally Posted by copeg View Post
    Curious where this is suggested. Do you have a link? Because from the standpoint of making code loosely couple, adaptable, and reusable this advice needs to be scrutinized.
    You are right. Oracle just points out use of inner classes is inefficient but is a good OO design. But does not recommend
    any of the methods
    We can not recommend a specific approach because one solution would not suit all situations.
    General Information about Writing Event Listeners (The Java™ Tutorials > Creating a GUI With JFC/Swing > Writing Event Listeners)
    It must be some pro-OO design website that I got the suggestion from. Among the three methods they compared
    (switches, annonymous classes, inner classes), they suggested inner classes as best OO practice.

  10. The Following User Says Thank You to dabdi For This Useful Post:

    ishtar (October 5th, 2011)

  11. #7
    Junior Member
    Join Date
    Sep 2011
    Posts
    10
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Default Re: Event Handling - Best Practice?

    Just a quick post to say thank you all

    Regards
    Nikki

Similar Threads

  1. Event Handling
    By maress in forum What's Wrong With My Code?
    Replies: 0
    Last Post: January 24th, 2011, 02:29 AM
  2. newbie GUI/ event handling problem
    By zyspt in forum What's Wrong With My Code?
    Replies: 1
    Last Post: May 10th, 2010, 11:36 AM
  3. [SOLVED] JRadioButton Event Handling Help?
    By CS313e in forum AWT / Java Swing
    Replies: 2
    Last Post: March 27th, 2010, 11:56 AM
  4. JButton event handling.
    By Prof in forum AWT / Java Swing
    Replies: 6
    Last Post: February 3rd, 2010, 09:29 AM
  5. Event handling
    By subhvi in forum AWT / Java Swing
    Replies: 3
    Last Post: August 26th, 2009, 11:20 AM