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.


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

Results 1 to 2 of 2

Thread: proper actionPerformed practice?

  1. #1
    Join Date
    May 2011
    west palm beach, FL
    My Mood
    Thanked 11 Times in 10 Posts

    Default proper actionPerformed practice?

    im trying to learn a lot about good coding practices and i see a lot of different ways people are doing the actionPerformed method...which one is better practice?

    button.addActionListener(new ActionListener()  {
    public void actionPerformed(ActionEvent e) {}

    public void actionPerformed(ActionEvent e) {
            //all buttons code sorted here

  2. #2
    Super Moderator
    Join Date
    Jun 2013
    So. Maryland, USA
    My Mood
    Thanked 698 Times in 680 Posts

    Default Re: proper actionPerformed practice?

    The first is adding the ActionListener by creating an anonymous inner class and is the 'better' practice. The second is a common way to demonstrate ActionListeners in a single program element or class, but if the design is used on more complex programs, the programmer will eventually discover the program is a tangled mess of code that is difficult to untangle and maintain. It is advised to separate the business (model), interface (view), and controller logic as much as possible. A common design pattern (or architecture) to accomplish this is known as the Model View Controller, or MVC, pattern.

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

    derekxec (August 4th, 2013)

Similar Threads

  1. actionPerformed Question.
    By TP-Oreilly in forum Java Theory & Questions
    Replies: 1
    Last Post: December 9th, 2011, 11:18 AM
  2. actionPerformed not returning a value.
    By gher in forum What's Wrong With My Code?
    Replies: 1
    Last Post: October 22nd, 2011, 10:15 AM
  3. Needed help in actionPerformed statements
    By S-NESH in forum What's Wrong With My Code?
    Replies: 7
    Last Post: January 7th, 2011, 06:36 PM
  4. actionPerformed method
    By Deprogrammer in forum What's Wrong With My Code?
    Replies: 3
    Last Post: December 4th, 2010, 02:29 AM
  5. actionPerformed
    By Kumarrrr in forum Java Theory & Questions
    Replies: 5
    Last Post: November 23rd, 2010, 09:08 AM