File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Beginning Java and the fly likes problem with actionListeners Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of REST with Spring (video course) this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "problem with actionListeners" Watch "problem with actionListeners" New topic

problem with actionListeners

Gabriel White
Ranch Hand

Joined: Mar 02, 2003
Posts: 233
I am trying to create a single method that holds all of the event listeners of my simple drawing class and when I try to incorporate the drawing thickness radio button listeners into that I have problems.

Below are a few snippets of code to show you what I am trying to do, and
possibly what I am doing incorrectly. What I end up with is the last radio
button to be selected is the one that is used (for drawing). So if I select 1 then I draw with 1, if I select 2 then it draws with a 2, if I select 4 then it draws with a 4. But when I try to go back to 2 or 1 it draws with a 4(the last one selected). I assumed that it was because this was a listener for mulitple actions and that I am adding them in at the same time and that I am creating a new JRadioButtonMenuItem everytime I add something (Basically I have N action listeners, one for each item I create. I have tried to pass this in as a parameter only to get the same results. Can I do this or is there really
not an elegant design option here? Ultimately I want to pull all of my
listeners into a seperate class (Not Inner), but currently I am testing it in a method.

Thanks in advance.
[ April 30, 2007: Message edited by: Gabriel White ]
Campbell Ritchie

Joined: Oct 13, 2005
Posts: 45286
Don't like the notion of a single class for all your Listeners. I seem to write about it frequently, most recently here in reply to a query by Natasha Henry.
I usually have a rule of thumb that you only use an anonymous inner class when you have one and only one Listener doing a particular task. In your case, however, because you have a button-creating method, you could make an exception to that rule. Go back to your original plan
//setThicknessButton() method (stuff moved to setupListeners commented out)

private JRadioButtonMenuItem setThicknessButton(String name, ButtonGroup
thicknessMenuItem = new JRadioButtonMenuItem(name, false);
public void actionPerformed(ActionEvent e)
return thicknessMenuItem;
All you have to do is to add the method you commented out as part of an anonymous inner class. Like this:-Notice I have changed the parameter to int width. It is easier to write "" + width than the parseInt() method. And the bit about '0' + width will work nicely as long as width < 10. Even though you are now supposed to pass an int to the setMnemonic method.
I agree. Here's the link:
subject: problem with actionListeners
It's not a secret anymore!