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 5 of 5

Thread: Event Handling Styles

  1. #1
    Member
    Join Date
    Jun 2011
    Posts
    94
    My Mood
    Amazed
    Thanks
    22
    Thanked 1 Time in 1 Post

    Default Event Handling Styles

    Hi,

    I was just trying to improve one of my projects and it is turning into something nice.

    I have a GUI and all of my events were handled there, with anonymous classes. Then I wondered if it is really the right one.

    As far as I know, there are three basic event handling methods (At least for a beginner);

    1- Anonymous classes,
    buttonOne.addActionListener(new ActionListener(){
    //implement the method
    }

    2- Inner classes,

    public class NotHandler{
    //methods
    private class HandlerandInner extends MouseAdapter{
    }
    }

    3- A separate class.
    public class One{
     
    }
     
     
    public class Two implements MouseListener{
    //methods etc.
    }

    Can you guys explain me where to use every one of them? Or do you know an article or tutorial about this? I looked up for tutorials but they were just showing how to implement. I need to know where to implement
    I know I can use every one of them in every situation, but in a specific situation, one should be more efficient than the others, right?


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Event Handling Styles

    To really understand this, you need to really understand the differences between anonymous classes, inner classes (and static inner classes), and "normal" classes.

    Keep in mind that inner classes (both anonymous and non-static named inner classes) keep an invisible reference to the outer class, which means that as long as the Listener is in scope, so is the outer class. This might not matter for you, but it's something to keep in mind.

    Static inner classes don't have that reference, but they can't access any non-static class members.

    Separate, "normal" classes might be overkill if the event handler is only one line of code, but then again keeping around references to the outer class might be overkill if you don't need it.

    In the end, just like all programming, there is no one right answer. The "correct" thing to do depends entirely on you, your context, and what you find most maintainable.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    Member
    Join Date
    Jun 2011
    Posts
    94
    My Mood
    Amazed
    Thanks
    22
    Thanked 1 Time in 1 Post

    Default Re: Event Handling Styles

    I see.

    So, you say any class which can see the inner class also has access to the outer class? Is the reverse true too?

    Back to my original question, I am doing fine with the anonymous classes actually. Adapters and only the needed methods are good for now.
    The main reason I asked this question is, I wanted to keep my GUI class only for GUI as much as possible. I don't know if this helps or prevents something but, it lets me deal with the code better. All my variables in GUI are private. So, getters and setters would create a big code. But then again, it still would be cleaner. I wonder if this would be a good practice in terms of performance (Keeping the GUI class pure GUI as much as possible).

    The other thing was the fact that I am not writing a code but I am experimenting. This means I always try to improve and change things. Like, I used text boxes to keep data, then decided to use JList and I had to write my FileSave class from scratch. When you do that, you tend to break things and if I keep every class as pure as I can keep. Then the separate classes seem logical. But like I said, with all those setters and getters...

    I recently stopped learning then doing and started learning while doing. I used to (and still do) come here and ask questions how to do, what's the theory etc. But I found that, if you build a project, and improve it in your mind then improve your code to catch your ideas is better for me. But still I have to ask questions like this.

    So, while writing this long post, I thought about those three implementations and here is my ideas:

    - Anonymous classes are good if you don't use the components for the same or similar purposes. That eliminates the other two options, because you won't be writing less codes with the other two since your components have nothing in common.
    - Inner classes would be suitable if your components have something in common, like reading a file, then doing the job. So you could save yourself from writing the file read codes twice or more. Of course, you could just have a fileRead method too...
    - The separate classes are the less appealing ones. I really cannot see any advantages of them. Actually, they have serious obstacles; I have to write tens of getters and setters. I have to pass the GUI to the eventHandling class as a parameter and also I have to create another object in the GUI class.

    So, in terms of serving my laziness, this is the case. But I also would like to know about their performance and memory terms. I am not writing a resource hungry app right now, but what if I decide to write for a low resource system?

  4. #4
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Event Handling Styles

    Like I said, inner classes keep around a reference to the outer class. That can be a memory issue. Separate classes are useful in that they don't do that, plus they allow you to invoke other methods on the instance later.

    Recommended reading: When To Use Nested Classes, Local Classes, Anonymous Classes, and Lambda Expressions (The Java™ Tutorials > Learning the Java Language > Classes and Objects)
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  5. The Following User Says Thank You to KevinWorkman For This Useful Post:

    beer-in-box (March 6th, 2013)

  6. #5
    Member
    Join Date
    Jun 2011
    Posts
    94
    My Mood
    Amazed
    Thanks
    22
    Thanked 1 Time in 1 Post

    Default Re: Event Handling Styles

    Thanks.
    I googled for the terms and read the tutorial.
    At least for my present project, I think anonymous classes which I have been using are the best practice. And I just don't need to make my GUI class 'pure' GUI.
    I'll poke this thread if I wonder anything else on the subject.

Similar Threads

  1. Help with event handling - beginner
    By kyasuleyman in forum AWT / Java Swing
    Replies: 10
    Last Post: May 2nd, 2012, 06:59 AM
  2. Event Handling - Best Practice?
    By ishtar in forum Java Theory & Questions
    Replies: 6
    Last Post: October 2nd, 2011, 08:52 AM
  3. Event Handling
    By maress in forum What's Wrong With My Code?
    Replies: 0
    Last Post: January 24th, 2011, 03:29 AM
  4. [SOLVED] JRadioButton Event Handling Help?
    By CS313e in forum AWT / Java Swing
    Replies: 2
    Last Post: March 27th, 2010, 11:56 AM
  5. Event handling
    By subhvi in forum AWT / Java Swing
    Replies: 3
    Last Post: August 26th, 2009, 11:20 AM