Results 1 to 5 of 5
  1. #1
    John5788 is offline Member
    Join Date
    Dec 2011
    Posts
    9
    Rep Power
    0

    Default Organization of actionListeners

    Hello,

    I am curious how other people organize their ActionListeners for Swing GUI projects. Specifically I want to know whats a good method of organizing my code (for readability and perhaps even memory usage) for my ActionListeners.

    First question is, how much code do you guys store in each ActionListener? I am finding myself writing lots and lots of code inside and I am moving towards having the ActionListener call a separate function to minimize the amount of code I have in the constructor of my JFrame. Is it a good practice to use the ActionListener as only 1 line of code to be passed to a larger function?

    Second, do you guys typically allocate one main ActionListener which contains a series of if statements to see which source it came from, then process each event? Or do you find yourselves creating new ActionListeners for every single Component in your UI?

    I am new to Swing development and I am wondering what the typical code organization looks like.
    Last edited by John5788; 12-28-2011 at 01:49 AM.

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default Re: Organization of actionListeners

    I'm not sure how the professionals do it, but in the past, I've created small anonymous inner classes for listeners that are simple, short, and not required to be added to more than one JButton. If the listener is a bit longer (very subjective mind you), I've created private inner classes for it, and if longer still, then I've used separate stand-alone classes for the listener.

    Lately with a larger program I've been experimenting with using small anonymous inner classes for all listeners, and having them call the method of a Control class object.


    For example:

    Java Code:
    public class JiffiView2 implements Viewable {
       //... code deleted for sake of brevity
    
       private JiffiControl control;
    
       public JiffiView2() {
          //... code deleted for sake of brevity
    
          addBtn.addActionListener(new ActionListener() {
             public void actionPerformed(ActionEvent e) {
                if (control != null) {
                   control.addAction();
                }
             }
          });
          equalBtn.addActionListener(new ActionListener() {
             public void actionPerformed(ActionEvent e) {
                if (control != null) {
                   control.equalsAction();
                }
             }
          });
       }
    
    
       public void setControl(JiffiControl control) {
          this.control = control;
       }
    
       //... code deleted for sake of brevity
    }
    The whole code can be found here: cant-change-background-my-content-pane-2
    Last edited by Fubarable; 12-28-2011 at 02:06 AM. Reason: link corrected

  3. #3
    John5788 is offline Member
    Join Date
    Dec 2011
    Posts
    9
    Rep Power
    0

    Default Re: Organization of actionListeners

    That is the way I currently have it with a bunch of small anonymous inner classes. I'm wondering, does that take more memory than using 1 large class that contains a large switch statement?

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default Re: Organization of actionListeners

    Quote Originally Posted by John5788 View Post
    That is the way I currently have it with a bunch of small anonymous inner classes. I'm wondering, does that take more memory than using 1 large class that contains a large switch statement?
    I think you need to re-think relative "costs". Which do you think is more important and will "cost" more in wasted time and debugging later: worrying about wringing the absolute fewest kbytes of memory footprint or creating code that is well designed with high cohesion and low coupling?

  5. #5
    John5788 is offline Member
    Join Date
    Dec 2011
    Posts
    9
    Rep Power
    0

    Default Re: Organization of actionListeners

    Thanks, I like your organization of MVC with the project.

    A question, when you have an ActionListener that spawns a new modal window (whether it be a dialog or new frame), does the View instantiate a new object and call .setVisible(true) on it? or do you have the Controller, or even Model carry that task out?

Similar Threads

  1. JFrames and JPanels with ActionListeners
    By atomant in forum New To Java
    Replies: 3
    Last Post: 11-30-2010, 08:26 PM
  2. Question about ActionListeners and JAR files
    By Psyclone in forum AWT / Swing
    Replies: 6
    Last Post: 03-20-2010, 01:08 PM
  3. Replies: 1
    Last Post: 12-07-2008, 06:50 PM
  4. [SOLVED] code organization
    By JT4NK3D in forum New To Java
    Replies: 1
    Last Post: 05-27-2008, 05:46 AM
  5. class organization question
    By maa11235 in forum New To Java
    Replies: 0
    Last Post: 12-17-2007, 04:25 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •