# I need SERIOUS Java help RE: action listeners

**URL:** <https://boards.straightdope.com/t/i-need-serious-java-help-re-action-listeners/85473>\
**Category:** Miscellaneous and Personal Stuff I Must Share\
**Created:** [October 5, 2001, 12:09am UTC](https://boards.straightdope.com/t/i-need-serious-java-help-re-action-listeners/85473 "2001-10-05T00:09:23Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![erislover](https://avatars.discourse-cdn.com/v4/letter/e/71e660/32.png) [@erislover](https://boards.straightdope.com/u/erislover)\
**Post date:** [October 5, 2001, 12:09am UTC](https://boards.straightdope.com/t/i-need-serious-java-help-re-action-listeners/85473/1 "2001-10-05T00:09:23Z")

</div>

Ok, I have been tearing my fucking hair out for the past three days trying to get a single fucking button respond to a generic event. It is the only button on the page.

I have seen so many different error messages that I can’t even begin to explain my frustration here.

I cannot seem to add a listener to the button because I have no clue where to put the class which acts as the interface. Is it the JFrame class which I create an instance of? Is it the class which adds all the stuff and creates the instance of the other class? Do I create an inner class in either of the two classes?

And, most importantly, since the actionPerformed method is declared void, and it must be inside an ActionEvent class, _how the fuck do I reference any component at all?_

I stare at my _two_ teach yer’self Java books with a dumb face. I page over and over through my Java almanac, trying to even get a feel for how these fucking things need to associate.

I got nothing, and I am starting to literally pound my keyboard so I guess its time to call in some help if it is out there.

---

<div class="post-metadata">

**Author:** ![TheNerd](https://avatars.discourse-cdn.com/v4/letter/t/858c86/32.png) [@TheNerd](https://boards.straightdope.com/u/TheNerd)\
**Post date:** [October 5, 2001, 1:25am UTC](https://boards.straightdope.com/t/i-need-serious-java-help-re-action-listeners/85473/2 "2001-10-05T01:25:32Z")

</div>

I don’t understand your point about which class acts as the interface. I assume you do not mean the technical meaning of interface.

ActionListeners are usually implemented as anonymous inner classes of the same component that contains the component they are listening to. For example, if you have a JFrame with a JButton you want to listen to with an action listener, you would instantiate your JButton, add it to the JFrame, and addListener(\< anonymous inner class definition \>).

Since it is an inner class defined in the JFrame, it should have access to all methods and variables that the JFrame does, that’s how you can access them.

I’ll look for some simple source code for you.

---

<div class="post-metadata">

**Author:** ![TheNerd](https://avatars.discourse-cdn.com/v4/letter/t/858c86/32.png) [@TheNerd](https://boards.straightdope.com/u/TheNerd)\
**Post date:** [October 5, 2001, 1:28am UTC](https://boards.straightdope.com/t/i-need-serious-java-help-re-action-listeners/85473/3 "2001-10-05T01:28:09Z")

</div>

[this page](http://www.javaworld.com/javaworld/javaqa/2000-03/02-qa-innerclass.html) has some explanations of inner classes and example code.

---

<div class="post-metadata">

**Author:** ![erislover](https://avatars.discourse-cdn.com/v4/letter/e/71e660/32.png) [@erislover](https://boards.straightdope.com/u/erislover)\
**Post date:** [October 5, 2001, 1:59am UTC](https://boards.straightdope.com/t/i-need-serious-java-help-re-action-listeners/85473/4 "2001-10-05T01:59:44Z")

</div>

Aww, I just got it.

What **erl** did right: almost nothing.  
What **erl** did wrong: no constructor methods for subclassing in order to get an instance of a window with a damn interface in it.

_sigh_

Now, if I can just get it to display what I want it to before I clik the button I’ll be ok.

Thanks for the attempt. I should note the ironic sentence in the page you linked:

> [@](#):
>
> As with anything else, you have to take the good with the bad. Inner classes have their disadvantages. From a maintenance point of view, inexperienced Java developers may find the inner class difficult to understand.

😛
