How to detect the origin Button of an ActionEvent?

Discussion in 'Java' started by Tom Parson, Nov 14, 2005.

  1. Tom Parson

    Tom Parson Guest

    Assume the user clicked on a button which in turn results in an ActionEvent e.
    Is there a way to detect in actionperformed() from which button the click was originally coming?

    A statement like

    public void actionPerformed(ActionEvent e){
    if (e.getComponent() = mybutton2) ...

    does not work.

    Tom
     
    Tom Parson, Nov 14, 2005
    #1
    1. Advertising

  2. Tom Parson

    Vova Reznik Guest

    Tom Parson wrote:

    > if (e.getComponent() = mybutton2) ...
    >


    if (e.getSource() = mybutton2)
     
    Vova Reznik, Nov 14, 2005
    #2
    1. Advertising

  3. Tom Parson wrote:

    > Assume the user clicked on a button which in turn results in an ActionEvent e.
    > Is there a way to detect in actionperformed() from which button the click was originally coming?
    >
    > A statement like
    >
    > public void actionPerformed(ActionEvent e){
    > if (e.getComponent() = mybutton2) ...

    You probably want
    if (e.getSource() == mybutton2) ...
    >
    > does not work.

    See also
    <http://java.sun.com/j2se/1.5.0/docs/api/java/awt/event/ActionEvent.html>
    and scroll down to "Methods inherited from class java.util.EventObject"

    --
    "Thomas:Fritsch$ops:de".replace(':','.').replace('$','@')
     
    Thomas Fritsch, Nov 14, 2005
    #3
  4. Tom Parson

    Mark Guest

    if (e.getSource() == mybutton2)

    Also, make sure that you have set up the proper listener for the
    selection event, something like:

    mybutton2.addActionListener(this);

    (Assumes your code is, for example, extending a JFrame, and that the
    JFrame is listening for selections of mybutton2, and that the JFrame you
    have extended has the actionPerformed() method.)


    Tom Parson wrote:
    > Assume the user clicked on a button which in turn results in an ActionEvent e.
    > Is there a way to detect in actionperformed() from which button the click was originally coming?
    >
    > A statement like
    >
    > public void actionPerformed(ActionEvent e){
    > if (e.getComponent() = mybutton2) ...
    >
    > does not work.
    >
    > Tom
    >
     
    Mark, Nov 14, 2005
    #4
  5. Tom Parson

    Guest

    Yes. You can use getSource(). It will give you a reference to the
    object that caused the event.

    Remember that when you use == between object references, it determines
    if the references point to the same object in memory.
     
    , Nov 14, 2005
    #5
  6. Followup-To ignored and reset to clj.gui.

    In comp.lang.java.gui Tom Parson <> wrote:

    > Is there a way to detect in actionperformed() from which button the click was originally coming?


    > A statement like


    > public void actionPerformed(ActionEvent e){
    > if (e.getComponent() = mybutton2) ...



    getSource()

    There are other things in the standard API that can fire ActionEvents,
    not just buttons (or components). Which ones?



    Christian
     
    Christian Kaufhold, Nov 14, 2005
    #6
  7. Tom Parson wrote:

    > Assume the user clicked on a button which in turn results in an ActionEvent e.
    > Is there a way to detect in actionperformed() from which button the click was originally coming?


    <http://java.sun.com/j2se/1.5.0/docs/api/java/util/EventObject.html#getSource()>
     
    Andrew Thompson, Nov 14, 2005
    #7
  8. Tom Parson wrote:
    > Assume the user clicked on a button which in turn results in an ActionEvent e.
    > Is there a way to detect in actionperformed() from which button the click was originally coming?
    >
    > A statement like
    >
    > public void actionPerformed(ActionEvent e){
    > if (e.getComponent() = mybutton2) ...
    >
    > does not work.


    Did you mean:

    public void actionPerformed(ActionEvent e){
    if (e.getComponent() == mybutton2) ...

    by any chance? If so, does it make a difference to your situation?
     
    Richard F.L.R.Snashall, Nov 14, 2005
    #8
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Nicol Nghia
    Replies:
    0
    Views:
    361
    Nicol Nghia
    Dec 3, 2005
  2. Florence HENRY

    Unable to dispatch an ActionEvent

    Florence HENRY, Jan 6, 2005, in forum: Java
    Replies:
    8
    Views:
    4,831
    Nigel Wade
    Jan 10, 2005
  3. Replies:
    1
    Views:
    343
    Daniel Pitts
    Feb 2, 2007
  4. damezumari
    Replies:
    3
    Views:
    837
    damezumari
    Nov 8, 2007
  5. Mark_Galeck
    Replies:
    3
    Views:
    619
    Mark_Galeck
    Jul 25, 2008
Loading...

Share This Page