Results 1 to 5 of 5
  1. #1
    enerj is offline Member
    Join Date
    Sep 2010
    Posts
    30
    Rep Power
    0

    Default Knowing what object called the actionListener?

    Hi I have been working on a simple game program that has a bunch of JButton's displayed in a panel, and each has the same actionListener associated with it.

    My end goal is to have the button that was pressed call a method in that objects class, except I can't find an easy way to get this object from the actionListener method.

    I actually just played with it while looking through my code, is this the best way to go about it?

    Java Code:
    private class Clicker implements ActionListener{
    	
    	public void actionPerformed(ActionEvent ae){
    		Tile thisTile =  (Tile) ae.getSource();
    		thisTile.changeColor();
    
    }

  2. #2
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    Have you read the ActionEvent API documentation? There's a getSource() method that may be useful.

    [Edit]

    Just read more closely! Yes, try getSource().

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

    Default

    Yes, you get the source of the button press via the ActionEvent's getSource method. Often though each group of buttons will have their own ActionListener, so this becomes less important.

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

    Default

    Have you tried to use and anonymous inner class?

    kind regards,

    Jos

  5. #5
    Tolls is online now Moderator
    Join Date
    Apr 2009
    Posts
    12,091
    Rep Power
    20

    Default

    Yep.
    If the partiocular button is important then, as Jos implies, an anonymous inner class is the way to go, because then you don't have to worry about which button it is...you already know.

    ETA: Of course, now that I've actually read your code (OK, OK, I should have done that first) an anonymous one is probably not a good idea if this is some game thing with a load of Tiles. :)

Similar Threads

  1. Replies: 2
    Last Post: 08-12-2010, 07:30 AM
  2. Need help in knowing how to clear buffer.
    By tedstevens in forum New To Java
    Replies: 1
    Last Post: 08-20-2009, 05:02 AM
  3. Knowing which IP address is active
    By sukatoa in forum Networking
    Replies: 2
    Last Post: 05-05-2009, 04:01 PM
  4. Using ActionListener with a Timer Object
    By bumblyb33 in forum New To Java
    Replies: 2
    Last Post: 03-04-2009, 11:26 PM
  5. Replies: 1
    Last Post: 07-26-2008, 05:26 PM

Posting Permissions

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