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: Question About Best Practice for ActionListener With Multiple Buttons

  1. #1
    Junior Member
    Join Date
    Jan 2014
    Posts
    8
    My Mood
    Sleepy
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Question Question About Best Practice for ActionListener With Multiple Buttons

    So if you have a program with 4-6 buttons on a toolbar is it better to give them each their own ActionListener with embedded ActionPerformed/ActionEvent handlers or should you implement an action listener then use one actionPerformed method to catch them all?

    In short...This....
    JButton button1 = new JButton("1");
       button1.addActionListener(new ActionListener() {
          public void actionPerformed(ActionEvent event) {
             ...
           }
        });

    or something like this....

    open_btn.addActionListener(this);
    close_btn.addActionListener(this);
    ...
    public void actionPerformed(ActionEvent e){
       if(e.getSource() == close_btn){
          System.exit(0);
       }else if(e.getSource() == open_btn){
       ...
       }
    }


  2. #2
    Member
    Join Date
    Mar 2012
    Location
    United States
    Posts
    118
    My Mood
    Inspired
    Thanks
    1
    Thanked 33 Times in 31 Posts

    Default Re: Question About Best Practice for ActionListener With Multiple Buttons

    As long as the code works as expected and is easily readable then either would be fine.

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

    OmegaNine (February 15th, 2014)

  4. #3
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Question About Best Practice for ActionListener With Multiple Buttons

    I believe the first approach is more widely used, though at least one programmer I respect said they never use anonymous inner classes. I'm not sure they had a good reason or whether it just boiled down to personal preference.

    Using 'this' as the actionListener as in the second approach is definitely the less elegant, used more typically by less experienced programmers, but the same code using a separate class - even an inner class - would be an improvement and just fine.

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

    OmegaNine (February 15th, 2014)

  6. #4
    Junior Member
    Join Date
    Jan 2014
    Posts
    8
    My Mood
    Sleepy
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: Question About Best Practice for ActionListener With Multiple Buttons

    Thanks for the quick responses. I figured it would boil down to a opinion thing. I have 6 buttons on a toolbar and the build method for it is starting to get longer and longer as I code each button and add the listeners. I wasn't sure if it would be a faux pas to just make one method with if else statements to clean up the toolbar construction method or not.

    Quote Originally Posted by GregBrannon View Post
    Using 'this' as the actionListener as in the second approach is definitely the less elegant, used more typically by less experienced programmers, but the same code using a separate class - even an inner class - would be an improvement and just fine.
    I guess I will steer away from this and create a class just to implement the actionlistener. It just seemed easier and much less code to do it that way. Plus then I was able to easily pass the JFrame as an object to change the status text on the bottom of it. I will rejigger the code a bit see if I can remove the this reference without adding too much complexity. To be fair though, I am the less experienced programmer that you speak of

    Thanks again all!
    Omega

  7. #5
    Senior Member
    Join Date
    Jul 2013
    Location
    Europe
    Posts
    666
    Thanks
    0
    Thanked 121 Times in 105 Posts

    Default Re: Question About Best Practice for ActionListener With Multiple Buttons

    You could also move the creation of annonymous action listeners to a helper method if you dont want to blow up your GUI creation method.

    for example something like this:
    public void initGUI() {
         Button btnFile = new Button();
         btn.addActionListener(get_file_actionListener());
     
         Button btnEdit = new Button();
         btn.addActionListener(get_edit_actionListener());
     
         Button btnAbout = new Button();
         btn.addActionListener(get_about_actionListener());
     
         Button btnHelp = new Button();
         btn.addActionListener(get_help_actionListener());
    }
     
    private ActionListener get_file_actionListener() {
         return new ActionListener() { ... };
    }
     
    private ActionListener get_edit_actionListener() {
         return new ActionListener() { ... };
    }
     
    private ActionListener get_about_actionListener() {
         return new ActionListener() { ... };
    }
     
    private ActionListener get_help_actionListener() {
         return new ActionListener() { ... };
    }

    Could maybe help to improve readability although in total it would be slightly more code. Still less code then creating a new actual class for each listener.

Similar Threads

  1. Wordsearch game - How to select multiple buttons/textviews in one swipe?
    By Stockholm Syndrome in forum Android Development
    Replies: 0
    Last Post: March 11th, 2013, 01:04 PM
  2. Swing GUI: Using buttons from a different classfile in ActionListener
    By VikingCoder in forum Java Theory & Questions
    Replies: 1
    Last Post: November 6th, 2012, 10:56 AM
  3. Help with actionListener and buttons
    By jm24 in forum AWT / Java Swing
    Replies: 10
    Last Post: February 11th, 2012, 01:50 PM
  4. Need help with this practice java question
    By ZenithX in forum What's Wrong With My Code?
    Replies: 4
    Last Post: October 24th, 2011, 04:26 PM
  5. Replies: 3
    Last Post: October 19th, 2010, 03:49 PM

Tags for this Thread