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

Thread: Anonymous inner class with Action Listener and Abstract Action

  1. #1
    Junior Member
    Join Date
    Nov 2013
    Posts
    6
    My Mood
    Cheerful
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Anonymous inner class with Action Listener and Abstract Action

    I already known about anonymous inner class for example,

    "I will try and post the code in a reply since I can't put it here because of, Post denied. New posts are limited by number of URLs it may contain and checked if it doesn't contain forbidden words."

    I was googling and found this method of doing basically the same thing.

    "I will try and post the code in a reply since I can't put it here because of, Post denied. New posts are limited by number of URLs it may contain and checked if it doesn't contain forbidden words."

    So with anonymous inner classes I have to define the text of the button before implementing the anonymous inner class and with the AbstractAction I can define the text of the button at the same time is that the only difference or is there something else?

    Any help would be greatly appreciated.

    Thank you,

    Steve

    --- Update ---

    JButton newGame = new JButton("New Game");
     
    newGame.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent event) {
                  // code
           }
    });


    --- Update ---

    I was googling and found this method of doing basically the same thing.

    newGame = new JButton(new AbstractAction("New Game") {
     
              @Override
               public void actionPerformed(ActionEvent event) {
                    //code
                }
    });


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,077
    Thanks
    47
    Thanked 2,147 Times in 2,120 Posts

    Default Re: Anonymous inner class with Action Listener and Abstract Action

    is that the only difference or is there something else?
    Have tried both those methods in code and executed it and did they do what you wanted?
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Nov 2013
    Posts
    6
    My Mood
    Cheerful
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Anonymous inner class with Action Listener and Abstract Action

    yes they do, maybe I should be more specific, Is there any efficiency benefits to one or the other or is it just style preference?

    thank you

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,077
    Thanks
    47
    Thanked 2,147 Times in 2,120 Posts

    Default Re: Anonymous inner class with Action Listener and Abstract Action

    I've never wondered about that. I'm sure that the newer stuff has more options and connections with other parts of Swing.
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member
    Join Date
    Nov 2013
    Posts
    6
    My Mood
    Cheerful
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Anonymous inner class with Action Listener and Abstract Action

    After doing some more research on the subject matter I have found out that,

    "The javax.swing.Action interface, and the corresponding class, javax.swing.AbstractAction, provide a useful mechanism to implement action listeners that can be shared and coordinated."

    source: Java: Action, AbstractAction

    Also after reading the API a little more I found out that the AbstractAction provides default implementations for the JFC Action interface.

    Also,

    "That Action implementations tend to be more expensive in terms of storage than a typical ActionListener"

    So I came to the conclusion that since I'm not using and don't have a need for the Action interface in my current program I should stick with the ActionListener.

    Thank you for the feedback,

    Steve

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,077
    Thanks
    47
    Thanked 2,147 Times in 2,120 Posts

    Default Re: Anonymous inner class with Action Listener and Abstract Action

    Good job doing the research. Thanks.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Accessing non-static methods from action listener in another class?
    By mfj in forum What's Wrong With My Code?
    Replies: 3
    Last Post: March 10th, 2013, 06:23 PM
  2. Action Listener Error
    By dookie1293 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: June 22nd, 2011, 09:34 PM
  3. Action Listener
    By Suzanne in forum What's Wrong With My Code?
    Replies: 7
    Last Post: May 29th, 2010, 10:50 AM
  4. Action Listener
    By kray66 in forum AWT / Java Swing
    Replies: 2
    Last Post: April 19th, 2010, 03:26 PM
  5. Need Help Action Listener....
    By vhizent23 in forum AWT / Java Swing
    Replies: 2
    Last Post: October 9th, 2009, 01:46 PM