Results 1 to 13 of 13
  1. #1
    Bimz is offline Member
    Join Date
    Aug 2011
    Posts
    31
    Rep Power
    0

    Default multiple event handlers

    okay so ive just finished a project which ive been working on for quite a while, a calculator, and as you probably know it has many button's so also many differant actionListeners. they all respond to being clicked but they must perform differant operations. atm i have about 15 differant actionListeners, which take a long time to code and make my code look really confusing, so i was wondering if there was a way i only had to write 1 actionListener, maybe using arrays for my JButtons. or something like that.

    help would be greatly appreciated, Bimz :)

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,306
    Rep Power
    25

    Default Re: multiple event handlers

    if there was a way i only had to write 1 actionListener, maybe using arrays for my JButtons.
    Yes you could do it that way.

    Each of the buttons has different actions to do so keeping the listeners separate keeps your logic cleaner.
    There is no mixing of code for different buttons with if tests to see which code section is to handle this button

  3. #3
    Bimz is offline Member
    Join Date
    Aug 2011
    Posts
    31
    Rep Power
    0

    Default Re: multiple event handlers

    so would i then use if statements inside of the actionListener to find out which button has been clicked, and then perform the action?
    .... also am i able to create an array of JButtons? if so please could you show me an example?

    thank you

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,306
    Rep Power
    25

    Default Re: multiple event handlers

    use if statements inside of the actionListener to find out which button has been clicked
    Yes, the event object has method that will give you a reference to the source of the event

    What do you want to use the array of JButtons for?

  5. #5
    Bimz is offline Member
    Join Date
    Aug 2011
    Posts
    31
    Rep Power
    0

    Default Re: multiple event handlers

    wait... blond moment ;)

    ur right i dont need an array of JButtons i can just refer to them by there names.... thank you :)

  6. #6
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,383
    Blog Entries
    7
    Rep Power
    20

    Default Re: multiple event handlers

    Compare the two scenarios:

    1) a single listener: in its actionPerformed( ... ) method you'll see a bunch of if - else if - else if - ... else statements because the listener has to determine which component fired the event.
    2) a listener per component: the listener 'knows' which component fired the event and perform its action without a lot of testing.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  7. #7
    Bimz is offline Member
    Join Date
    Aug 2011
    Posts
    31
    Rep Power
    0

    Default Re: multiple event handlers

    im remaking the calculator from scratch, and this is what i have so far:

    setVisible(true);

    HandlerClass handler = new HandlerClass();

    btn0.addActionListener(handler);

    }

    public class HandlerClass implements ActionListener {

    public void ActionPerformed(ActionEvent event) {

    }

    }


    this only has the actionListener for the first button, but how would i go about making the handler class?

  8. #8
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,383
    Blog Entries
    7
    Rep Power
    20

    Default Re: multiple event handlers

    Quote Originally Posted by Bimz View Post
    this only has the actionListener for the first button, but how would i go about making the handler class?
    Only you know what is supposed to happen when the btn0 button is pressed. If the action is simple you can even make an anonymous class for the listener:

    Java Code:
    btn0.addListener(new ActionListener() {
       public void actionPerformed(ActionEvent ae) {
          // action code for btn0 press here ...
       }
    });
    you have to add a listener for each and every button in your calculator. Depending on the functionality of those listeners you have to write a separate (anonymous) class for the ActionListeners.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  9. #9
    Bimz is offline Member
    Join Date
    Aug 2011
    Posts
    31
    Rep Power
    0

    Default Re: multiple event handlers

    ok, thank you.

    would i be able to write multiple if statements in the HandlerClass, and each if statement have its own overiding class in it depending on what the utton has to do?

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

    Default Re: multiple event handlers

    Myself, I would make one ActionListener for each group of buttons that does the same thing. So for instance, if I were creating a calculator, I'd create one ActionListener for the number buttons. No big if switchboard would be needed since I'd be able to get the number from the button's text and do what needs to be done with it. I'd probably create another ActionListener for the basic math operators (+, -, *, /), one for the memory operations, etc...

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

    Default Re: multiple event handlers


  12. #12
    Bimz is offline Member
    Join Date
    Aug 2011
    Posts
    31
    Rep Power
    0

    Default Re: multiple event handlers

    thanks for the help :) but i think this is a bit too hard for me :(

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

    Default Re: multiple event handlers

    Quote Originally Posted by Bimz View Post
    thanks for the help :) but i think this is a bit too hard for me :(
    Only if you let it be. Push yourself hard though and you may just amaze yourself. :)

Similar Threads

  1. multiple Data from Jsp that having same name of multiple input tage to servlet
    By rahul9323.2007@gmail.com in forum Advanced Java
    Replies: 0
    Last Post: 07-29-2011, 01:00 PM
  2. Replies: 9
    Last Post: 01-20-2011, 04:01 PM
  3. checking for an event during an event
    By infinity in forum AWT / Swing
    Replies: 22
    Last Post: 04-09-2009, 01:08 AM
  4. Multiple listeners per event
    By Java Tip in forum Java Tip
    Replies: 1
    Last Post: 01-03-2008, 10:06 AM
  5. Help with Event Handlers
    By Daniel in forum AWT / Swing
    Replies: 2
    Last Post: 07-04-2007, 06:16 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
  •