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

Thread: Question about ActionListener

  1. #1
    Junior Member
    Join Date
    Sep 2009
    Location
    Mount Morgan, Queensland, Australia
    Posts
    12
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Question about ActionListener

    Hi guys.

    This is just a question I've been meaning to ask about ActionListeners. Is it wrong, or even bad form, to have multiple listeners for the one program? The way I've taken to writing code is to use one listener for each button, if each button is in it's own inner class. Any way, one program I'm screwing around with has around 20 or so listeners in it. Is there anything wrong with this, or would it be better to stick to the one listener?


  2. #2
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: Question about ActionListener

    What you mean by having 20 or so listeners? I'm not sure what Java's behavior is if you add a single object multiple times to the same object that's sending out ActionEvents, but I know that's completely unnecessary. However, if you have 2 different buttons, and you want your program to listen to both buttons, you MUST add your main program to the ActionListeners list of each button. Also, there's nothing wrong with having each button listen to each other, just make sure your main program isn't duplicating the actionsPerformed by these two buttons listening to each other.

  3. #3
    Junior Member
    Join Date
    Sep 2009
    Location
    Mount Morgan, Queensland, Australia
    Posts
    12
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Question about ActionListener

    What I mean is, each button is located on an individual panel and each panel has its own listener. This allows me to copy any panel I want into another program if I need to without any alteration.
    Example:

    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
     
     
    public class Example extends JFrame
    {
    	public Example()
    	{
    		add(thePanel());
    	}
     
    	public JPanel thePanel()
    	{
    		JPanel panel = new JPanel(new GridLayout(2,1));
    		panel.add(panel1());
    		panel.add(panel2());
    		return panel;
    	}
     
    	public JPanel panel1()
    	{
    		JPanel panel = new JPanel(new GridBagLayout());
    		JButton button = new JButton("Button 1");
    		class buttonListener implements ActionListener
    		{
    			public void actionPerformed(ActionEvent e)
    			{
    				//Button does something
    			}
    		}
    		ActionListener list = new buttonListener();
    		button.addActionListener(list);
    		panel.add(button);
    		return panel;
    	}
     
    	public JPanel panel2()
    	{
    		JPanel panel = new JPanel(new GridBagLayout());
    		JButton button = new JButton("Button 2");
    		class buttonListener implements ActionListener
    		{
    			public void actionPerformed(ActionEvent e)
    			{
    				//Button does something
    			}
    		}
    		ActionListener list = new buttonListener();
    		button.addActionListener(list);
    		panel.add(button);
    		return panel;
    	}
     
    	public static void main(String[] timw)
    	{
    		JFrame frame = new Example();
    		frame.setVisible(true);
    		frame.setSize(200,200);
    		frame.setDefaultCloseOperation(frame.EXIT_ON_CLOSE);
    		frame.setTitle("Example");
    	}
    }

  4. #4
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: Question about ActionListener

    Personally, I don't see anything wrong with that style-wise.

  5. #5
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,343
    Thanks
    182
    Thanked 831 Times in 774 Posts
    Blog Entries
    5

    Default Re: Question about ActionListener

    I have wondered this as well. I think it just depends upon how you wish to organize your code. I agree with helloworld...nothing at all wrong with doing it this way. This method does however seem harder to adapt (at least to me) should you wish to listen to the same command coming from different places. For example suppose you have a menu item and a button that you wish to perform the same action, you can adapt your inner classes to do this, but to me it seems a bit more difficult than just having a general listener that 'listens' to many different objects based upon action commands. In addition, creating a general listener makes it easier (easier at lest the way I see it) to take advantage of inheritance. For example:
    //Parent Class
    public class AppFrame extends JFrame implements ActionListener{
     
        public void actionPerformed(ActionEvent e){
            if ( e.getActionCommand().equals("Something") ){
                doSomething();
            }else  if ( e.getActionCommand().equals("Else") ){
                doElse();
            }
        }
    }
    //Child Class
    public class NextFrame extends AppFrame implements ActionListener{
       public void actionPerformed(ActionEvent e){
            if ( e.getActionCommand().equals("Something") ){
                doSomthingElse();
                super.actionPerformed(e);
            }else{
                super.actionPerformed(e);
            }
        }
    }

    NextFrame can perform some action before allowing its parent class to act, and in addition NextFrame doesn't even have to worry about the "Else" command because it is passed onto the parent. Granted this can be accomplished either way you do it, I just find I can wrap my head around it easier this way
    Last edited by copeg; November 4th, 2009 at 09:15 AM.

  6. #6
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: Question about ActionListener

    You want to be careful if you do that... the parent class doesn't necessarily have to implement ActionListener. Also, you have a totally unrelated class that may want to listen to that class's action events. I'd implement an addActionListener method in your class, and then send the event only to those that are listening to that panel.

    ArrayList<ActionListener> toSendTo;
     
    ...
     
    public void addActionListener(ActionListener listener)
    {
         if (listener != null)
              toSendTo.add(listener);
    }
    public void actionPerformed(ActionEvent e){
            if ( e.getActionCommand().equals("Something") ){
                doSomthingElse();
                super.actionPerformed(e);
            }else{
                for (int i = 0; i < toSendTo.size(); i++)
                {
                     toSendTo.actionPerformed(e);
                 }
            }
    }

  7. #7
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,343
    Thanks
    182
    Thanked 831 Times in 774 Posts
    Blog Entries
    5

    Default Re: Question about ActionListener

    Quote Originally Posted by helloworld922 View Post
    the parent class doesn't necessarily have to implement ActionListener.
    I think I'm misunderstanding the point you are making....can you clarify?

    Quote Originally Posted by helloworld922 View Post
    Also, you have a totally unrelated class that may want to listen to that class's action events. I'd implement an addActionListener method in your class, and then send the event only to those that are listening to that panel.
    Of course that would be one way if you want to have multiple listeners, although not the way I would want do it (but I rarely add multiple listeners so my experience in that realm is low)
    Last edited by copeg; November 4th, 2009 at 10:10 AM.

Similar Threads

  1. How to Add ActionListener to a JButton in Swing?
    By JavaPF in forum Java Swing Tutorials
    Replies: 17
    Last Post: April 24th, 2013, 05:14 PM
  2. Please help me with a research question
    By Tom in forum The Cafe
    Replies: 4
    Last Post: October 31st, 2009, 02:22 PM
  3. A question about an array
    By faizana2006 in forum Collections and Generics
    Replies: 2
    Last Post: October 28th, 2009, 04:36 PM
  4. SOMEONE PLEASE HELP ME ADD THE ACTIONLISTENER INTERFACE...
    By beginning2Understand in forum AWT / Java Swing
    Replies: 5
    Last Post: June 30th, 2009, 12:42 AM
  5. [SOLVED] Difference in the code on changing logical operators
    By lotus in forum Loops & Control Statements
    Replies: 3
    Last Post: June 20th, 2009, 03:30 AM