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: How do you add two actionListeners (in a GUI)?

  1. #1
    Member
    Join Date
    Sep 2013
    Posts
    64
    Thanks
    24
    Thanked 0 Times in 0 Posts

    Default How do you add two actionListeners (in a GUI)?

    Like, I have one button: submit.

    When I click this button, I take in the data and stuff.

    Then I have a second button: done.

    When I click this button, I close everything down.


    I don't know how to have each button do it's own thing.

    More specifically, here's the issue:

     JPanel p = new JPanel();
                    p.setLayout(new GridLayout(5,5));  
     
     
                    // add ActionListener for this submit button
                    JButton submit = new JButton("Submit");
                    p.add(submit);
                    submit.addActionListener(this);
     
                    // add ActionListener for this done button
                    JButton done = new JButton("Done");
                    p.add(submit);
                    done.addActionListener(this);


    Which all looks good but.....

    the action listener definition is the same for both!!!!


     public void actionPerformed(ActionEvent e)
            {
              // do submit stuff
            }
     
            public void actionPerformed(ActionEvent e)
            {
               // do done stuff
             }


    This works with just the submit button but now I want to add the done button and I don't know what to do!!!


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    21,824
    Thanks
    55
    Thanked 2,344 Times in 2,315 Posts

    Default Re: How do you add two actionListeners (in a GUI)?

    Several ways to do it:
    1) Create an anonymous class for each button
    2) The object that created the ActionEvent has a reference in the object. In the class's actionPerformed() method call the ActionEvent object's getSource() method to get that reference and compare it against references to each button.
    3)Create unique classes that extend ActionListener for each button.
    If you don't understand my answer, don't ignore it, ask a question.

  3. The Following User Says Thank You to Norm For This Useful Post:

    ineedahero (December 12th, 2013)

  4. #3
    Member
    Join Date
    Sep 2013
    Posts
    64
    Thanks
    24
    Thanked 0 Times in 0 Posts

    Default Re: How do you add two actionListeners (in a GUI)?

    Ok so I want to go off your third option!!

    I played around with it in my head for a while and I'm a little confused?

    So I'm thinking it ought to look something like this?

                    JPanel p = new JPanel();
                    p.setLayout(new GridLayout(5,5));  
     
     
                    SubmitButton sb = new SubmitButton();
                    p.add(sb.submit);
     
                    DoneButton db = new DoneButton();
                    p.add(db.done)

    And then, way down, the classes themselves:

              private class SubmitButton extends ActionListener
              {
                       JButton submit = new JButton("Submit"); // submit button as the data member
     
                      public SubmitButton()
                      {
                           submit.addActionListener(this);
                      }
     
                      public void actionPerformed(ActionEvent e) // action event that its implementing
                      {
                            // do submit stuff
                      }
     
             private class DoneButton extends ActionListener
             {
                    JButton done = new JButton("Done"); // done button as the data member
     
                     public DoneButton()
                     {
                          done.addActionListener(this);
                     }
     
                     public void actionPerformed(ActionEvent e) // action event that its implementing
                     {
                            // do done stuff
                     }
              }

    Is that even close to right?? I never did anything like this before and I have no idea lol. It makes sense to me but I'm sure there's something wrong

  5. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    21,824
    Thanks
    55
    Thanked 2,344 Times in 2,315 Posts

    Default Re: How do you add two actionListeners (in a GUI)?

    The normal contents for a class that implements ActionListener is the actionPerformed() method. Nothing more.
    Create an instance of that class and use that in the addListener() method.
    If you don't understand my answer, don't ignore it, ask a question.

  6. The Following User Says Thank You to Norm For This Useful Post:

    ineedahero (December 12th, 2013)

  7. #5
    Member
    Join Date
    Sep 2013
    Posts
    64
    Thanks
    24
    Thanked 0 Times in 0 Posts

    Default Re: How do you add two actionListeners (in a GUI)?

    Ah, so, among the other problems, I have to IMPLEMENT the ActionListener -- not extend it, correct?

    --- Update ---

    Ooohhhh I got it working!!

    What you said was really helpful!!! thanks a lot!

  8. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    21,824
    Thanks
    55
    Thanked 2,344 Times in 2,315 Posts

    Default Re: How do you add two actionListeners (in a GUI)?

    Glad you got it working.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Connecting Two Different ActionListeners
    By steme in forum Java Theory & Questions
    Replies: 1
    Last Post: April 26th, 2013, 09:35 AM
  2. Minimalist GUI, do I need to add anything to Eclipse IDE?
    By cowboytaketwo in forum Java IDEs
    Replies: 1
    Last Post: December 7th, 2011, 02:00 PM
  3. [SOLVED] Trying to add scrollbar to gui
    By illidari in forum What's Wrong With My Code?
    Replies: 2
    Last Post: July 13th, 2011, 11:38 PM
  4. Replies: 1
    Last Post: April 26th, 2011, 09:47 AM
  5. How do you add GUI to this java program?
    By leyla in forum AWT / Java Swing
    Replies: 1
    Last Post: October 18th, 2009, 02:32 PM