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

Thread: How to use multiple actionlisteners

  1. #1
    Member
    Join Date
    Sep 2011
    Posts
    46
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default How to use multiple actionlisteners

    Hi,
    I am trying to make a code that has multiple buttons, but the only way i know how to make action listeners are to either use inner classes (which require variable to be final, which isnt good for me), or to use AddActionListener(this), which only allows for one action listener.
    Ive heard somehting about getSource(), but cant find any good examples of how its used... Are there any other ways to use multiple action listeners?

    This is a code i was experimenting with getSource on, but i kept on getting an error:
    import java.awt.*;
    import javax.swing.*;
    import java.awt.event.ActionListener;
    import java.awt.event.ActionEvent;
    public class FindAddPage implements ActionListener
    {
        public static void main (String[] args)
        {
            int counter = 0;
            //set up frame
            GridLayout myLayout = new GridLayout (2,1);
            JFrame myFrame = new JFrame ("Add/Find");
            myFrame.setSize(400,200);
            JPanel myPanel = new JPanel();
            myPanel.setLayout(myLayout);
     
            //make buttons
            JButton add = new JButton ("Add A Tutor");
            JButton find = new JButton ("Find A Tutor");
     
            if(event.getSource()==add){
                counter++;
            }
     
            myFrame.setVisible(true);
        }
    }


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: How to use multiple actionlisteners

    You can also make an inner class that extends ActionListener, create an instance of that and add it as a listener.
    Does your usage of getSource() work? Where is the object: event defined and given a value?
    Do a search on the forum for getSource() for some code samples.

  3. #3
    Junior Member
    Join Date
    Nov 2011
    Location
    tanzania
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: How to use multiple actionlisteners

    May be if our forum Vip means this try it
    public void actionPerformed(ActionEvent event)
    {
    if(event.getSource()==add){
    counter++;}
    }

  4. #4
    Think of me.... Mr.777's Avatar
    Join Date
    Mar 2011
    Location
    Pakistan
    Posts
    1,136
    My Mood
    Grumpy
    Thanks
    20
    Thanked 82 Times in 78 Posts
    Blog Entries
    1

    Default Re: How to use multiple actionlisteners

    What does event.getSource() returns???
    Why don't you try casting it?

  5. #5
    Member
    Join Date
    Sep 2011
    Posts
    46
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: How to use multiple actionlisteners

    Hi,
    I tried using this:
     ActionListener al = new ActionListener(){
            public void actionPerformed(ActionEvent event){
                if(event.getSource()==add){
                    counter++;
                }
            }
     };

    However it still gave me the error that "add" and "counter" would need to be made final... Is there a way to get around this?

  6. #6
    Junior Member
    Join Date
    Nov 2011
    Location
    tanzania
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: How to use multiple actionlisteners

    try to make add and counter Class variable

  7. #7
    Think of me.... Mr.777's Avatar
    Join Date
    Mar 2011
    Location
    Pakistan
    Posts
    1,136
    My Mood
    Grumpy
    Thanks
    20
    Thanked 82 Times in 78 Posts
    Blog Entries
    1

    Default Re: How to use multiple actionlisteners

    Well, it's saying you coz i guess you are drawing buttons in your main() method, am i right?
    If so, remember, main() is a static method and you can't reference the object from static context like that.
    Well, can you give your whole code? Or is that all?

  8. #8
    Think of me.... Mr.777's Avatar
    Join Date
    Mar 2011
    Location
    Pakistan
    Posts
    1,136
    My Mood
    Grumpy
    Thanks
    20
    Thanked 82 Times in 78 Posts
    Blog Entries
    1

    Default Re: How to use multiple actionlisteners

    I will suggest you some things.
    1. Draw the GUI in constructor.
    2. And then add an actionevent to the button, you want to.
    3. Make an object of GUI class in your main and set it to visible.

  9. #9
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: How to use multiple actionlisteners

    Move the code out of the main method into the constructor for the class and have the only code in the main method call that constructor. Then you can use class variables.

  10. #10
    Member
    Join Date
    Sep 2011
    Posts
    46
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: How to use multiple actionlisteners

    Hey,
    I got it to work. I made the GUI in a constructor method and then used main to create the new GUI. Thank You for the help

Similar Threads

  1. [SOLVED] Quick question regarding ActionListeners
    By Stockholm Syndrome in forum Java Theory & Questions
    Replies: 2
    Last Post: October 4th, 2011, 12:02 PM
  2. multiple jvms
    By gaikwaddeepali111 in forum Member Introductions
    Replies: 0
    Last Post: September 10th, 2011, 12:22 AM
  3. Multiple Problems
    By ZeroLRS in forum What's Wrong With My Code?
    Replies: 6
    Last Post: August 16th, 2011, 07:33 AM
  4. [SOLVED] NullPointerException on ActionListeners
    By cherryduck in forum What's Wrong With My Code?
    Replies: 2
    Last Post: December 7th, 2010, 04:17 PM
  5. multiple tcp connection?
    By nasser in forum Java Networking
    Replies: 4
    Last Post: July 31st, 2010, 07:35 AM