Results 1 to 9 of 9
  1. #1
    Billaguana is offline Member
    Join Date
    Jan 2011
    Posts
    21
    Rep Power
    0

    Default two different Listeners

    I am trying to add a toolbar with one button on it and already have another button on the window.
    Java Code:
    tool1 = new JToolBar();
    		tool1.add(button1);
    		button1.addActionListener(new Listener());
    		add(tool1);
    and


    Java Code:
    JButton button = new JButton("Calculate");
    		button.addActionListener(new Listener());
    		add(button);
    so my problem is that i want one to do a separate action than the other but they both use the same listener. So my question is where and what do i need to put in to have two separate listeners.

    thanks.

  2. #2
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,788
    Rep Power
    7

    Default

    You can create 2 different Listener classes and attach the appropriate one to each of the buttons. Or you can use the one Listener and in the actionPerformed method use an if statement to determine which button got pressed. Or you can use anonymous classes.
    Java Code:
    button1.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent event) {
            // add code for this buttons action
            doStuffForButton1();
       }
    });
    
    button2.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent event) {
            // add code for this buttons action
            doStuffForButton2();
       }
    });

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

  4. #4
    Billaguana is offline Member
    Join Date
    Jan 2011
    Posts
    21
    Rep Power
    0

    Default

    thanks you junky but i am liking fubarable's solution better. I was trying to achieve this but how do i tell actionPerformed which is different?

  5. #5
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,788
    Rep Power
    7

    Default

    I'm still feeling in a good mood.
    Java Code:
    if(event.getSource() == button1) {
    
    } else if(event.getSource() == button2) {
    
    }

  6. #6
    Billaguana is offline Member
    Join Date
    Jan 2011
    Posts
    21
    Rep Power
    0

    Default

    o ok, thank you. didnt mean to put you in a bad one.

  7. #7
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,788
    Rep Power
    7

    Default

    No, it's not that.

    I just don't usually provide code because I think people learn better by going away with a hint provided and try and write the code themselves. Then if they come back they can post their code and we can provide further hints as to how to fix it.

  8. #8
    Billaguana is offline Member
    Join Date
    Jan 2011
    Posts
    21
    Rep Power
    0

    Default

    o, ok. well thanks anyway. I dont usually get it when beating around the bush so thanks. I guess last thing would be is their a way to make it so that you have two seperate
    Java Code:
    public void actionPerformed(ActionEvent e)
    		{
    		}
    with something else in the ActionEvent e slot that will tell it weather to use that for the button pushed or not. something like
    Java Code:
    public void actionPerformed(button1) {
    
    stuff for this button
    
    }
    public void actionPerformed(button2)  {
    
    
    stuff for this button
    }
    is that at all possible? and thanks for the help

  9. #9
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,788
    Rep Power
    7

    Default

    No you cannot do that. I repeat what I said in my first post with a bit of clarification.

    Option 1: Two classes.
    Java Code:
    class ListenerOne implements ActionListener {
        public void actionPerformed(ActionEvent event) {
            ....
        }
    }
    
    class ListenerTwo implements ActionListener {
        public void actionPerformed(ActionEvent event) {
            ....
        }
    }
    
    buttonOne.addActionListener(new ListenerOne());
    buttonTwo.addActionListener(new ListenerTwo());
    Option 2: One listener
    Java Code:
    class Listener implements ActionListener {
        public void actionPerformed(ActionEvent event) {
            if(event.getSource() == button1) {
                ....
            } else if(event.getSource() == button2) {
                ....
            }
        }
    }
    
    Listener listen = new Listener();
    buttonOne.addActionListener(listener);
    buttonTwo.addActionListener(listener);
    Option 3: Anonymous classes as per my code in my first reply

Similar Threads

  1. Listeners and events
    By cglacet in forum New To Java
    Replies: 3
    Last Post: 01-20-2011, 02:21 PM
  2. key listeners
    By zjames in forum New To Java
    Replies: 22
    Last Post: 11-24-2010, 05:58 AM
  3. Can't implement listeners
    By xael in forum New To Java
    Replies: 3
    Last Post: 09-02-2010, 06:33 PM
  4. Help with Listeners
    By Psyclone in forum AWT / Swing
    Replies: 8
    Last Post: 02-09-2010, 07:21 PM
  5. Seriously need help on my listeners!!
    By themburu in forum Java Applets
    Replies: 4
    Last Post: 05-26-2008, 10:41 AM

Posting Permissions

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