Results 1 to 5 of 5
  1. #1
    Join Date
    Nov 2010
    Location
    New Cross, London, UK
    Posts
    18
    Rep Power
    0

    Default Best way to implement an ActionListener.

    Hi all, basically, I have two classes. One which creates my GUI with Swing, and one which will handle all of my logical operations. What I want to know is, what is the best way of implementing the ActionListener? I have the following in my GUI class

    Java Code:
    //Addition of ActionListeners
            quitGame.addActionListener(new ActionListener() 
            {
                public void actionPerformed(ActionEvent arg0)
                {
                    System.exit(0);
                }
            });
    Am I best off doing this multiple times in the class, or am I better off using an inner class?

    Thanks in advance.

    Jack.

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

    Default

    Quote Originally Posted by VisionIncision View Post
    Am I best off doing this multiple times in the class, or am I better off using an inner class?
    Not to be glib, but you're better off using whatever you think works best for the situation. Myself, if I have a button with a unique independent action, I usually use an anonymous inner class. If I have a bunch of buttons whose actions require the same logic, such as the numeric buttons of a calculator, I have them share the same ActionListener, possibly one implemented as a private inner class or a stand-alone class. If my logic is in another class, then I sometimes have the actionPerformed call the model's method.

    Please understand that I'm no expert and am not saying that my opinion is the only correct one, and in fact far from it.

  3. #3
    Join Date
    Nov 2010
    Location
    New Cross, London, UK
    Posts
    18
    Rep Power
    0

    Default

    Quote Originally Posted by Fubarable View Post
    Not to be glib, but you're better off using whatever you think works best for the situation. Myself, if I have a button with a unique independent action, I usually use an anonymous inner class. If I have a bunch of buttons whose actions require the same logic, such as the numeric buttons of a calculator, I have them share the same ActionListener, possibly one implemented as a private inner class or a stand-alone class. If my logic is in another class, then I sometimes have the actionPerformed call the model's method.

    Please understand that I'm no expert and am not saying that my opinion is the only correct one, and in fact far from it.
    Thanks for the response, from that I take it I am best off carrying on the way I am? The methods that I will be invoking are not all "the same" as entering digits on a calculator would be. Also, what do you mean by an "anonymous" inner class?

    Sorry for all the questions, I started my CS course in September.

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

    Default

    Quote Originally Posted by VisionIncision View Post
    ...what do you mean by an "anonymous" inner class?
    An anonymous inner class is an object created from a class that is never given a name. e.g.,
    Java Code:
          JButton doItBtn = new JButton("Do It");
    
          // the class below implements ActionListener, but has no name.
          doItBtn.addActionListener(new ActionListener() {
             public void actionPerformed(ActionEvent e) {
                doItActonPerformed();
             }
          });

  5. #5
    Join Date
    Nov 2010
    Location
    New Cross, London, UK
    Posts
    18
    Rep Power
    0

    Default

    Quote Originally Posted by Fubarable View Post
    An anonymous inner class is an object created from a class that is never given a name. e.g.,
    Java Code:
          JButton doItBtn = new JButton("Do It");
    
          // the class below implements ActionListener, but has no name.
          doItBtn.addActionListener(new ActionListener() {
             public void actionPerformed(ActionEvent e) {
                doItActonPerformed();
             }
          });
    Ah I get it. Thanks.

Similar Threads

  1. ActionListener w/ Dialog
    By BariMutation in forum New To Java
    Replies: 1
    Last Post: 12-03-2010, 05:50 PM
  2. ActionListener NOT working
    By zed420 in forum New To Java
    Replies: 3
    Last Post: 12-20-2009, 04:40 PM
  3. Java actionlistener help
    By justsomeguy in forum AWT / Swing
    Replies: 1
    Last Post: 05-27-2008, 06:42 AM
  4. How to use KeyListener and ActionListener
    By Java Tip in forum javax.swing
    Replies: 0
    Last Post: 04-23-2008, 09:19 PM
  5. ActionListener interface
    By tsantana in forum New To Java
    Replies: 2
    Last Post: 03-30-2008, 11:24 PM

Tags for this Thread

Posting Permissions

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